Sure, you can add content to a given WooCommerce transactional email such as the Customer Processing Order email. You can even edit the email subject, for example by adding the first name. No matter what you need, there are many cool snippets to customize the default WooCommerce emails.
But what we haven’t seen yet is how to “attach a file” to a WooCommerce Order email. It seems like a very much needed admin option, but there is nothing to achieve that in the WooCommerce dashboard. And maybe that is because it’s super easy to do that with a few lines of PHP or a super simple plugin. Enjoy!
PHP Snippet: Attach File to WooCommerce Order Emails
First of all, you need to upload the file somewhere. In my case, I decided to upload it via the Media > Add New section of the WordPress dashboard. You can also choose to place the file in your child theme folder for example, in which case you will need to work with get_stylesheet_directory().
In the example below, my file is called example.pdf and has been placed under /wp-content/uploads/2020/09/example.pdf – inside the snippet you will see the wp_upload_dir() function that is needed to return the uploads folder “path” (not the absolute URL otherwise it won’t work).
Also, I decided to attach such file only in the “new_order” and “customer_processing_order” emails. You can pick yours, and you can find the email IDs here: https://www.businessbloomer.com/woocommerce-add-extra-content-order-email/
/**
* @snippet File Attachment @ WooCommerce Emails
* @how-to Get CustomizeWoo.com FREE
* @author Rodolfo Melogli
* @testedwith WooCommerce 7
* @community https://businessbloomer.com/club/
*/
add_filter( 'woocommerce_email_attachments', 'bbloomer_attach_pdf_to_emails', 10, 4 );
function bbloomer_attach_pdf_to_emails( $attachments, $email_id, $order, $email ) {
$email_ids = array( 'new_order', 'customer_processing_order' );
if ( in_array ( $email_id, $email_ids ) ) {
$upload_dir = wp_upload_dir();
$attachments[] = $upload_dir['basedir'] . "/2020/09/example.pdf";
}
return $attachments;
}
Mini-Plugin: Business Bloomer WooCommerce Attach Files To Order Emails
You don’t feel confident with coding? You don’t want to purchase yet another bloated, expensive plugin? Great!
Business Bloomer WooCommerce Attach File To Order Emails is a mini WooCommerce plugin, without the usual hassles. One feature. Lifetime license. No annoying subscriptions. 1 plugin file. A few lines of code. No banners. No up-sells. No WP notifications. Use it on as many websites as you like. Lifetime support. 1-page documentation. No admin dashboard.
Screenshot of the settings:
Quick demo:
As you can see the settings are pretty straight forward. Select a WooCommerce email from the dropdown, and add the attachment URL beside it. Add as many attachments as you wish. Simple!
Hi Rodolfo,
is it also possible to attach the pdf invoice to the completed order? because this pdf invoice is not in the media library.
Thank you and regards, York
Hello York. Doesn’t your PDF invoice plugin let you attach the invoice to whatever WooCommerce email out of the box?
Hi
Does this snippet only work with .pdf files? I need to send .docx
Works with any file extension, as long as the email client doesn’t block it (for example, Gmail doesn’t allow for .ade, .adp, .apk, .appx, .appxbundle, .bat, .cab, .chm, .cmd, .com, .cpl, .diagcab, .diagcfg, .diagpack, .dll, .dmg, .ex, .ex_, .exe, .hta, .img, .ins, .iso, .isp, .jar, .jnlp, .js, .jse, .lib, .lnk, .mde, .msc, .msi, .msix, .msixbundle, .msp, .mst, .nsh, .pif, .ps1, .scr, .sct, .shb, .sys, .vb, .vbe, .vbs, .vhd, .vxd, .wsc, .wsf, .wsh, .xll)
Hi Rodolfo,
This is a pearl!! I have been looking for…! But I think it conflicts with my plugin Woocommerce Invoice?
My site crashes when I add this in and the file path is correct and the file in place 🙂
Maybe it’s using the same var names or so?
Is there some way to change the names or is it the “return [dollar-sign]attachments;” that only is allowed once?
WooCommerce Invoices do attach a invoice to certain mails (customer_Completed).
WooCommerce Invoices can attached the terms page as well – But it looks crap (all the shortcode from Enfold get printed)
So I created a TERMS page to attach with the invoice mail (two attachments) as well… That is the file I want to add
Best Regards
Netzie
Hi Peter, what error do you get?
Hi,
Once an order is placed, I’d like to add the packing-slip PDF automatically generated, and attach it to the first email the customer receives after ordering.
So by what file could I replace your line with example.pdf ?
Thanks
Patrick
Hi Patrick! Your packing slips plugin should already give you that option?
He Rodolfo,
is it possible to specify the snippet to a specific product? So the attachment is only send when this specific product is in the order?
Hi Cees-Jan, sure, that’s possible. I suggest you take a look at “conditional logic”: https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Enjoy 🙂
The Snippet does not work. Ive attached it to functions.php and it doesnt work
Is the file URL correct? Can you share your code?
Hello,
It’s working great, nice snippet. Thank you.
Cool
Hey!
First of all, it works fine!
But i have a problem: The attachment is identified as Phishing and the email ends up in the spam folder (i use sendinblue).
Any thoughts on this?
Not sure. Does that happen even if you attach a JPG for example?
16th July 2021. this is not working for me. anyone else having any issues
Works for me. Can you try disable all plugins but Woo?
Not working may I know whats the problem
Works for me. Have you tested on a fresh WP install with only Woo active and a default theme?
It works partially. I (the seller) receive the email with the attachment but customer receives an email without the attachment. Why is that?
ok, customer_on_hold_order is the answer.
Ok!
Thanks a lot! How to add two files?
Hi Artur, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hello,
Can I send a dynamically generate pdf file using this? I mean pdf name is varying.
Hi Krishna, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hello, what if we need to add different PDFs as attachments to the “Completed Order” email, e.g. I use external software to generate invoices and I’d like to send them to the clients with the “Completed Order” email?
Uhm not sure
Hello men,
Your site has helped us countless times.
And now he would help us if you give us code for multiple pdf files per email order.
Thx
Bojan
Hi Bojan, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hello Rodolfo,
as usual you are such an helpful ressource when it comes to customise WooCommerce. This works like a charm !
All the bests
Yay!
What if I wanted to attach different invoices for each specific order, when adding a new order in WooCommerce instead of a default attachment for every new order? How would I go about that?
Hi David, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Yo Rodolfo,
What about if we have dynamically created file, which have to be deleted after download?
Hey Tesla, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!