Add a custom note to your WooCommerce emails. Use actions instead of changing templates. It can be simple or complex.
WooCommerce sends emails to customers and site admin at multiple points during the order process. Just like WooCommerce pages, the content of these emails can be modified with actions, filters or by modifying the templates. Using actions and filters is the better choice because they rarely change while it is not unusual for templates to be changed and require development work to upgrade any modification you have made to your local copies.
I am working on a site where one category of products is made to order and therefore are not shipped out the same day like other products. While the information is in the product description I want to add it to the order emails so that the customer is reminded of this difference in delivery time.
Order Email Layout
Many of the WooCommerce order emails follow the same layout with 4 do_action() calls.
Each email has a PHP class associated with it. Different email classes attach different functions to these 4 actions.
Add the Note
I used the ‘‘woocommerce_email_order_details‘ action with a low priority to display it above the order contents.
If your note is generic and applies to all orders, your code will be super simple. My note was only if the order contained products from a specific category.
Check order for products
The action passes 4 parameters to your function – the order object ($order), whether the email is being sent to the admin ($sent_to_admin), whether it is the html or plain text email being sent ($plain_text) and finally the email object ($email).
The admin didn’t need to see the note so my code returns immediately if being sent to the admin.
Next is goes through each item in the order. It checks if the product is in the ‘shapetrousers‘ category. For variable products ($product->is_type( ‘variation’ )) it gets the parent product before checking the category because variations are children of the main product.
The message displayed in plain text emails is simply the html message without the markup.
Going Further: The Email Object
The fourth parameter in the action is the email object. You can examine this to change the emails that your code runs on.
For example, to add a note to only the Processing Order email you would examine the $email->id value.