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 default WooCommerce emails…
But what we haven’t seen yet is how to “attach a file” to a default 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. So, 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 tutoraspire.com FREE
* @author Tutor Aspire
* @testedwith WooCommerce 4.5
* @donate $9 https://tutoraspire.com
*/
add_filter( 'woocommerce_email_attachments', 'tutoraspire_attach_pdf_to_emails', 10, 4 );
function tutoraspire_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;
}