Posted on

Adding email attachments in WooCommerce

WooCommerce is a very great platform for developers since the source code provides hooks almost everywhere. It means default behaviors and datas can be easily altered by your own code. But for regular users / shop owners, that means “hidden” features you can’t access without a plugin.

For instance, i often receive requests from people who would like to use one of my plugin in order to add attachments to their transactional emails in WooCommerce. Since the plugin is not designed primarily for that here is a little snippet you can use to achieve that.

Note that this code relies on WooCommerce and you don’t need any extra plugin to do that. WordPress also offers a filter for email attachments but this one gives us WooCommerce specific context so it can be used to add different attachment on a per-email basis : completed order, on-hold order etc…

You will have to upload your attachment to your WordPress media library then get its ID to make it work.

How do i get the ID of an attachment ?

Go to media > Add new, then after the upload has finished click on the “edit” link button and you will find the ID in url (something like/wp-admin/post.php?post=988&action=edit) where 988 is the ID you will have to use in your code.

Code explained

woocommerce_email_attachments is the filter we are using to achieve what we want : adding attachments to WooCommerce specific emails. So we need to “hook” a custom function in order to manipulate that data. That’s how a WordPress filter works : the filter gives us a data and we send it back to where it belongs once manipulated. In addition, filters often provides extra variables to give us more insight and context so we can actually modified the data to the desired value.

In our case, we can see in the WooCommerce source code (woocommerce/includes/emails/class-wc-email.php line 290), that the id of the email and the email object are both provided to our function in addition to the attachement array itself.  (array(), $this->id, $this->object)

So back in our exemple, we are testing the id of the email – in fact the id represents which email we are dealing with – to attach media ID number 612 to the on-hold order email only.

Where to i put that code ?

The best way to use that kind of code snippets is to create a “Must Use Plugin”. Simply create an empty PHP file you can for instance named : woocommerce-attachment-email.php, put the code in it and save it in wp-content/mu-plugins/ folder. Maybe you will need to create that folder if it doesn’t not exist.

Code customization

Since my code is only a working exemple for on-hold order email. Here’s is a list of the different WooCommerce emails id / context :

  • cancelled_order
  • customer_completed_order
  • customer_invoice
  • customer_new_account
  • customer_note
  • customer_on_hold_order
  • customer_processing_order
  • customer_reset_password
  • failed_order
  • new_order

Leave a Reply

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