Adding Attachments To WooCommerce Emails


About The Author
Avatar Cabe Nolan is the founder of WP Cover where he shares his insight into WordPress, development, & entrepreneurship. Outside of WPCover, Cabe continues to run a successful WordPress development firm, Bold City Design as well as a few high profile websites, Arrivala, Two Way Resume, Dock Skipper, and a successful outdoors brand, DolfinPack.







Adding Attachments To WooCommerce Emails

WooCommerce is a great e-commerce platform that comes with flexibility. One request we recently had from a client was adding a PDF document to one of the automated WooCommerce emails that gets sent out. I went hunting for the correct filter and happy to share it today. Here is the code that would go in your functions.php file, I’ll explain each part of it below:
 

add_filter( 'woocommerce_email_attachments', 'wpcover_woocommerce_attachments', 10, 3 );
function wpcover_woocommerce_attachments($attachments, $email_id, $email_object){
	if( $email_id === 'customer_processing_order' || $email_id === 'customer_on_hold_order'){
		$attachments[] = get_attached_file( 2527 );
	}
	return $attachments;
}

 
Before you implement the above code, you’ll need to upload your attachment to the WordPress media library. Once uploaded, you would grab the attachment ID and swap it for the number 2527 as seen in the example above on line 4.

You may also want to adjust line 3 of the example function that determines which emails the PDF is attached to. In this case, we’re attaching it to the order processing and order on hold emails. Here is a list of other WooCommerce emails you may want to attach to:

customer_processing_order
cancelled_order
customer_completed_order
customer_invoice
customer_new_account
customer_note
customer_on_hold_order
customer_refunded_order
customer_reset_password
failed_order
new_order

I hope the above helps somebody else who is looking to attach PDFs or other document types to the automated emails WooCommerce sends out.


Share Your Thoughts

Leave a Reply

Your email address will not be published. Required fields are marked *


Related Stuff You Might Like







WordPress News, Tips, & Code Snippets

Join the WP Cover mailing list and get wordpress news, tips, code snippets, security warnings, and more delivered right to your inbox.  We won't flood your inbox, newsletters typically go out every 1-2 weeks unless it involves an important security release.

You have Successfully Subscribed!