Sometimes you want to add order notes to WooCommerce completed order emails, thankfully that's rather easy and only requires a little bit of programming. Even better, you can use WooCommerce's built in tools to make these changes!
UPDATE: Newer (cleaner and better) code available for the WooCommerce Complete Order Email! Check it out here: https://piotrkrzyzek.com/better-code-woocommerce-order-complete-email-notes/
Adding order notes can be a little bit weird in WooCommerce because there are several places you can put them into and several ways to retrieve them. Thankfully it's not difficult.
While you technically can add the order notes to any WooCommerce e-mail you recommend only putting it into these emails:
- Order Completed
- Processing Order
- Customer Note
- Customer Invoice
Of course, that depends on your workflow. For our needs, all we needed was to change the Order Completed e-mail to include order notes as that's where the Shipping information was put by our back-end accounting/shipping system.
Very important thing to know about WooCommerce ‘notes': WooCommerce marks every action on an order as a note. That means a note is creates once the order is created, once it's changed from accepted to processing, once the inventory is decreased by one because of this order, ect…
So, that means we (most likely) don't want to put every single one of those notes into the e-mail to a client/customer. All we want are the actual order notes (taken at order in the special notes field) and any shipping and tracking information added as a note.
Where to add the code
WooCommerce has a nice (and easy to use) system for modifying email templates without having to use any other tools. Login as admin, go to your WooCommerce settings, then go to the Emails tab. In the general email tab it will tell you where your email templates are located if you want to edit them through some other program, but for the purpose of this tutorial we'll use their built-in tools.
Under the main settings tabs, you'll see the e-mail settings links such as Email Options, New Orders and others. Click on “Completed Order”. If you haven't edited these templates yet, then on the bottom of the page click on “Copy file to theme”.
After that click on “View Template”. A little box will scroll open where you'll be able to make changes to the email template code. Sadly it doesn't have line numbers you'll have either to approximate where things are. These are small files so you should be able to find the correct places easily.
The code for displaying notes is slightly different than displaying comments. I mention this because if you are a developer you'll understand what I'm about to talk about, if you're not a programmer then ignore the next sentence. While products are technically just custom post types and therefore have comments, if we pull comments from a product we'll end up pulling one too many things and possible pull the wrong thing; so we have to stick to getting the order notes by going through the proper WooCommerce functions as it will then ONLY pull customer notes and any notes we've put into the system and ignore automated notes such as order changed.
You'll want to place the code right under lines #47 – #49 on the Order Complete e-mail template while looks like this:
You'll want to add the follow (I'll explain the code right below this):
get_customer_order_notes(); foreach($comments as $comment) : echo $comment->comment_content . '
'; endforeach; ?>
The above code will get you something like this:
Quick code explanation
This outputs the heading. You can change “Order Notes” to be whatever you'd want it to be.
This is the code that actually gets the non-system generated notes from the order:
$comments = $order->get_customer_order_notes();
Here we just run through each comment and make sure to print it on it's own line.
foreach($comments as $comment) : echo $comment->comment_content . '
Hopefully this has worked for you. There is a closing thing to think about (it's an issue I came up with when trying to make this work):
If you have a back-end system that runs shipping to your warehouse make sure that the system puts in the note FIRST and only then changes the order to completed. If you change status first before adding the note then you'll be sending e-mails with blank “order note” sections and customers won't get shipping details!