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:
- WC_Email_Cancelled_Order (learn more)
- WC_Email_Customer_Completed_Order (learn more)
- WC_Email_Customer_Invoice (learn more)
- WC_Email_Customer_New_Account (learn more)
- WC_Email_Customer_Note (learn more)
- WC_Email_Customer_On_Hold_Order (learn more)
- WC_Email_Customer_Processing_Order (learn more)
- WC_Email_Customer_Refunded_Order (learn more)
- WC_Email_Customer_Reset_Password (learn more)
- WC_Email_Failed_Order (learn more)
- WC_Email_New_Order (learn more)
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:
Hello,
what I’m doing wrong? Emails doesn’t get send. I’m using a child theme, its activated in WordPress und files are under subfolder directly in the child-theme.
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() . ‘/ressources/agb.pdf’;
$file_path_2 = get_stylesheet_directory() . ‘/ressources/Datenschutzerklärung.pdf’;
$file_path_3 = get_stylesheet_directory() . ‘/ressources/Wiederrufsbelehrung.pdf’;
if ( $email_id == ‘customer_on_hold_order’ || $email_id == ‘customer_completed_order’ || $email_id == ‘WC_Email_Customer_New_Account’ ){
$attachments[] = $file_path;
$attachments[] = $file_path_2;
$attachments[] = $file_path_3;
return $attachments;
} else {
return $attachments;
}
}
thank you
Chris
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;
}
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
You’re absolutely right! Thanks a lot.
Thanks! I’ve eddited the snippets.
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
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’;