You may want to attach files to WooCommerce order emails for example to send the customer the terms and conditions, the privacy policy, special thank you note, an important instruction, return application, promo code as image, the list is endless. So lets get started!

PHP Snippet: Add Attachment to WooCommerce order emails

To add an attachment to your order emails, open the functions.php file of your child theme and add the following snippet of code:

add_filter( 'woocommerce_email_attachments', 'webroom_attach_to_wc_emails', 10, 3);
function webroom_attach_to_wc_emails ( $attachments , $email_id, $order ) {

	// Avoiding errors and problems
    if ( ! is_a( $order, 'WC_Order' ) || ! isset( $email_id ) ) {
        return $attachments;
    }
  
	$file_path = get_template_directory() . '/file.pdf'; // directory of the current theme

 	// if you are using a child theme, use this line instead to get the directory
 	// $file_path = get_stylesheet_directory() . '/file.pdf';

	$attachments[] = $file_path;
	return $attachments;
}

We’re using .pdf in the example, but you can attach pretty much everything (.jpg, .png and so on).

Note: The location of the file in the snippet above is in the active theme you are using. If you are not using child theme, upload your file to the theme’s folder. So upload your file to: /themes/YOUR-THEME/file.pdf

If you are using a child theme comment out line 4 and use line 7 instead. So upload your file to: /themes/YOUR-CHILD-THEME/file.pdf

You can make sub folder in the theme’s folder to better organize things. For example if you upload your file to: /themes/YOUR-CHILD-THEME/resources/file.pdf remember to change the path in the snippet above to ‘/resources/file.pdf’

PHP Snippet: Adding Several Attachments to WooCommerce order emails

Adding two or more attachments to the WooCommerce emails is fairly easy, just create new variable with the file path and duplicate line 9 from the above snippet:

add_filter( 'woocommerce_email_attachments', 'webroom_attach_to_wc_emails', 10, 3);
function webroom_attach_to_wc_emails ( $attachments , $email_id, $order ) {

	// Avoiding errors and problems
    if ( ! is_a( $order, 'WC_Order' ) || ! isset( $email_id ) ) {
        return $attachments;
    }
  
	$file_path = get_template_directory() . '/file.pdf'; // directory of the current theme
	$file_path_2 = get_template_directory() . '/file.pdf'; // directory of the current theme
  
 	// if you are using a child theme, use this line instead to get the directory
 	// $file_path = get_stylesheet_directory() . '/file.pdf';

	$attachments[] = $file_path;
  	$attachments[] = $file_path_2;
	return $attachments;
}

PHP Snippet: Add different attachment to the different WooCommerce emails

May be it’s a good idea to have different attachment to the different emails the customers receives. This includes the following WooCommerce emails:

In order to differentiate the emails lets further customize the snippet. For the example we’ll check if the emails is On Hold, Completed Order or a Customer Note.

add_filter( 'woocommerce_email_attachments', 'webroom_attach_to_wc_emails', 10, 3);
function webroom_attach_to_wc_emails ( $attachments , $email_id, $order ) {

	// Avoiding errors and problems
    if ( ! is_a( $order, 'WC_Order' ) || ! isset( $email_id ) ) {
        return $attachments;
    }
  
	$file_path = get_stylesheet_directory() . '/1.jpg'; // directory of the current theme
	$file_path_2 = get_stylesheet_directory() . '/2.pdf'; // directory of the current theme
	$file_path_3 = get_stylesheet_directory() . '/3.pdf'; // directory of the current theme

 	// if a child theme is being used, then use this line to get the directory
 	// $file_path = get_stylesheet_directory() . '/file.pdf';
  
	if ( $email_id == 'customer_on_hold_order' ){
		$attachments[] = $file_path;
		return $attachments;
	} elseif ( $email_id == 'customer_completed_order' ) {
		$attachments[] = $file_path_2;	
		return $attachments;		
	} elseif ( $email_id == 'customer_note' ) {
		$attachments[] = $file_path_3;	
		return $attachments;		
	} else {
		return $attachments;
	}
}

Note: The type of email ($email_id == ‘customer_note’) must be in lower case. You get it from the list with WooCommerce emails above.

To check if the mail is WC_Email_Customer_Refunded_Order, the check must be $email_id == ‘customer_refunded_order’

You can even further customize: by sending attachment if a specific product is in the order. Learn how to check if the WooCommerce order contains a product or product category.

Learn more about the hook woocommerce_email_attachments we used in this tutorial here.

Why should one attach files to the order emails?

Share your thoughts on the topic in the comments section below.




Recent Articles

Blog Categories

Related Articles

If you enjoyed reading this, then please explore our other articles below:

Comments

Leave a comment
  1. Leydi July 15, 2020 at 12:19 am - Reply

    Hello, I did everything as is, I just changed the name of the pdf that I put in my topic.
    And it does not work…
    Doesn’t send the file I tell you … Did I do something wrong?

    add_filter( ‘woocommerce_email_attachments’, ‘webroom_attach_to_wc_emails’, 10, 3);
    function webroom_attach_to_wc_emails ( $attachments , $email_id, $order ) {

    // Avoiding errors and problems
    if ( ! is_a( $order, ‘WC_Order’ ) || ! isset( $email_id ) ) {
    return $attachments;
    }

    $file_path = get_template_directory() . ‘/libro_A1.pdf’; // directory of the current theme

    // if you are using a child theme, use this line instead to get the directory
    // $file_path = get_stylesheet_directory() . ‘/libro_A1.pdf’;

    $attachments[] = $file_path;
    return $attachments;
    }

  2. Mark June 22, 2020 at 3:52 pm - Reply

    Hi,

    Many thanks for this, just what I was looking for, however I believe you have a bit of a bug in your code. In the method calls you have this:
    function webroom_attach_to_wc_emails ( $attachments , $email_id, $object )

    a bit further down you have this:

    if ( ! is_a( $order, ‘WC_Order’ ) || ! isset( $email_id ) ) {

    I believe the $order in that second snippet should be referencing the $object in the first snippet.

    KR
    Mark

    • Luke June 23, 2020 at 1:44 pm - Reply

      You’re absolutely right! Thanks a lot.

    • webroom June 23, 2020 at 2:10 pm - Reply

      Thanks! I’ve eddited the snippets.

    • Marshall August 24, 2020 at 7:37 am - Reply

      Hi I had the same problem and changed the snippet but its still not working am I doing something wrong. I am using a child theme and folders to hold the documents.

      // Woocommerce adding email attachments open

      add_filter( ‘woocommerce_email_attachments’, ‘webroom_attach_to_wc_emails’, 10, 3);
      function webroom_attach_to_wc_emails ( $attachments , $email_id, $object ) {

      // Avoiding errors and problems
      if ( ! is_a( $object, ‘WC_Order’ ) || ! isset( $email_id ) ) {
      return $attachments;
      }

      //$file_path = get_template_directory() . ‘woocommerce/pdf/jasperpdf/file.pdf’; // directory of the current theme
      //$file_path_2 = get_template_directory() . ‘woocommerce/pdf//jasperpdf/file.pdf’; // directory of the current theme

      // if you are using a child theme, use this line instead to get the directory
      $file_path = get_stylesheet_directory() . ‘woocommerce/pdf/resources/terms.pdf’;
      $file_path_2 = get_stylesheet_directory() . ‘woocommerce/pdf/resources/privacy.pdf’;

      $attachments[] = $file_path;
      $attachments[] = $file_path_2;
      return $attachments;
      }

      // Woocommerce adding email attachments closed

      • Antonio September 16, 2020 at 7:16 am - Reply

        Try adding slash in path
        Not working: $file_path = get_stylesheet_directory() . ‘woocommerce/pdf/resources/terms.pdf’;
        Working: $file_path = get_stylesheet_directory() . ‘/woocommerce/pdf/resources/terms.pdf’;

Leave a Reply

Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.