Add an extra column to the WooCommerce order email.
I was asked how to add a column to the order email, to list the pre tax price. I replied that it’s not trivial as it will involve editing template files (something I try to avoid doing as it can become an maintenance nightmare).
Part way there with a filter
One of challenges with template editing is that the file is copied to the theme folder. What happens if you change theme but want to continue using the edited template file?
Another challenge is what to do when the WooCommerce template file is updated. The copied template file will need to be reviewed to see if the change requires an update to the template file. This is not trivial for the non-programmer.
With this in mind I first went looking for a filter. I found ‘woocommerce_email_order_item_quantity‘ in templates/emails/email-order-items.php. As the next content after the filter is a closing </td> I could add a new cell by returning it in the filter function e.g.
return $item_qty . '</td><td>' . $my_new_cell_content;
This breaks the table header and the subtotal section at the bottom. So, I need to modify a different template file: emails/email-order-details.php.
Small changes to template file
As I said earlier I try to avoid editing template files. If I have to I make as few changes as possible.
In email-order-details.php I made two changes: add the new column header and change colspan of the <tfoot> cell from 2 to 3.
It would be great if those change were possible via a filter e.g. change the header cells into an array and have a filter for the <tfoot> cells. I might submit a patch/pull request.
Adding the actual extra content without using the filter is in the email-order-items.php template file. I just add a new cell there.
Template file in a plugin
When I present code I try to always provide a plugin that is easy to use. I don’t suggest editing the theme’s functions.php. To include WooCommerce template files I use a filter to point to the file within the plugin directory.
For my demonstration the new column lists the product’s categories. I didn’t know where to access the pre-tax price but it should be easy to change my code for that scenario.
As using the ‘woocommerce_email_order_item_quantity‘ filter is a hack, I have disabled it and made a copy of email-order-items.php and added the code there (search the file for “New cell listing product categories” comment).
Then add the column header and change colspan:
And finally add the cell with the product’s categories: