Home / WooCommerce / How to Add an Attachment to WooCommerce Order Emails

How to Add an Attachment to WooCommerce Order Emails

Published On: March 5th, 2020|Categories: WooCommerce|Tags: , , |4 min read|

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.




Related Articles

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

More Articles

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