• Resolved AudunBK

    (@audunbk)


    I would like to attach a PDF to the confirmation email for product ID 11871. I found the following code. Will it work if I add in to functions.php in my Child theme for sending the attachment lederveiledning.pdf located in the root folder of my Child theme?

    function attach_terms_conditions_pdf_to_email ( $attachments, $status , $order ) {
    
    $allowed_statuses = array( 'new_order', 'customer_invoice', 'customer_processing_order', 'customer_completed_order' );
    
    if( isset( $status ) && in_array ( $status, $allowed_statuses ) ) {
    
        $attachment_products = array(11871) // id of product that will trigger email
        $send_email = false;
        $order_items = $order->get_items();
    
        foreach ($order_items as $item) { // loop through order items
            if(in_array($item['product_id'], $attachment_products)) { // compare each product id with listed products ids
                $send_email = true;
                break; // one match is found ; exit loop
            }
        }
    
        if($send_email) {
            $your_pdf_path = get_stylesheet_directory() . '/lederveiledning.pdf';
            $attachments[] = $your_pdf_path;
        }
    } 
    return $attachments; 
    }
Viewing 9 replies - 1 through 9 (of 9 total)
  • Hi there!

    I think that you could use a simpler approach using the “woocommerce_email_attachments” filter to add your PDF attachments to the default WooCommerce notification emails.

    Here’s a tutorial and a Stackoverflow thread with more details:

    https://www.mbcreation.com/adding-email-attachments-in-woocommerce/
    https://stackoverflow.com/questions/41632921/woocommerce-email-attachment

    Also, please mind that there’s an official WooCommerce plugin designed for this same purpose: https://woocommerce.com/products/email-attachments/

    I hope it helps!

    Thread Starter AudunBK

    (@audunbk)

    Thanks for the reply! I looked at each of the links, but could not see that they mentioned the case where I want to attach a PDF only if the used by a specific product. Where in the code do I tell it to look for the ID of the product that shall trigger the email?

    madeincosmos

    (@madeincosmos)

    Automattic Happiness Engineer

    Hi @audunbk,

    That’s a great question! Depending on the email type, the woocommerce_email_attachments filter will accept the email object as a parameter. For any order emails this will be the order object. You can use it to find the order and then iterate over all order items to find the one with ID 11871. Here’s a crude example of how this could look like:

    
    add_filter( 'woocommerce_email_attachments', 'add_woocommerce_attachments_for_certain_product', 10, 3 );
    
    function add_woocommerce_attachments_for_certain_product ( $attachments, $email_id, $email_order ){
      $product_id = 11871;
      $attachment_id = 99999;
    
      if( $email_id === 'customer_processing_order' ){
        $order = wc_get_order( $email_order );
        $items = $order->get_items();
      
        foreach ( $items as $item ) {
          if ( $product_id === $item->get_product_id() ) {
            $attachments[] = get_attached_file( $attachment_id );		
          }
        }	 
    	
      }
      return $attachments;
    }
    

    $attachment_id in the example here is the ID of attachment file in media library: https://cld.wthms.co/UlKsIw

    Having that said, WooCommerce has an option in plugin settings to attach a downloadable file and automatically email it to the customer when they buy the product:

    https://cld.wthms.co/8BJokv

    Is there any special reason for not doing this?

    Thread Starter AudunBK

    (@audunbk)

    Thanks @madeincosmos for helping me to solve this, I did like you recommended through the plugin settings.

    @madeincosmos

    Hello there, your code is working very good. Would it be possible to use different products instead of only one? The attachement will be the same for all.

    Thanks in advance,

    Alvaro

    @indigorise: Yes, you could modify the code to check for multiple product IDs

    For example, change:
    $product_id = 11871;
    to
    $product_id = array( 11871, 123, 456 ); // Add the product IDs (or variation IDs)

    And change:
    if ( $product_id === $item->get_product_id() ) {
    to
    if ( in_array( $item->get_product_id(), $product_id ) ) {

    Excellent. Thank you very much!

    @madeincosmos

    Thank you so much for the snippet. I wonder if you can help me a little further?

    I have an online store and I also sell practical training courses through woocommerce. I have a few courses with different product id’s that I want to attach a Terms & Conditions pdf to. Each course has a different terms & conditions.

    How would you set the code up to have say 5 different product id’s and 5 different attachment id’s? Each product id will match a different attachment id.

    @daymobrew

    Thanks! and how to attach multiple files. For instance an instruction manual and a maintenance manual.

    Best, Chris

    • This reply was modified 4 years, 4 months ago by cdegeus.
Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Attach PDF to confirmation email for specific product’ is closed to new replies.