Add WooCommerce Order Notes to Completed Emails From Your WordPress Store
There’s nothing better than having information about an order you made regarding its status or any other data that you can find useful regarding your order. By default WooCommerce stores every action linked to an order in the database. This series of actions is available in the administration, in the sidebar when you edit an order. But your customers don’t have access to this actions list. The good news is that you can easily add it to any email sent by WooCommerce. In this post, I will show you add to add the order notes the complete email (it’s the email sent when the order status is marked as completed). But you can use that method to add the order notes to any other emails as well.
Custom templates or hooks?
Just a quick reminder: all emails within WooCommerce are provided using templates, and you can override default templates by creating your own templates. Basically a custom template allows you to override WooCommerce default files and use your own custom files instead. Here is a quick tutorial that will explain you how to create your custom templates: https://docs.woothemes.com/document/template-structure/.
You can add the order notes within a custom templates, but i would like to show you another way to do it, using a hook. The idea is to not use custom templates, but to use the functions.php file within your theme folder only.
Order notes are WordPress comments
Order notes are WordPress comments having a specific type “order_note”, so you can use the WordPress WP_Query class applied to comments using the get_comments() function.
The hook we need to use is an action called “woocommerce_email_order_meta”. The idea is to call a custom function when this action is loaded in the WooCommerce process.
Using get_comments() simplifies a lo the way we can retrieve the order notes, we just need to make sure that we want to list the comments linked to a specific order using the post ID and the “approve” comment attribute which means that the comment is validated (by WooCommerce in this case).
The code
Here is the code to place within the functions.php file in your theme folder:
add_action( 'woocommerce_email_order_meta', 'woo_add_order_notes_to_email' );
function woo_add_order_notes_to_email() {
global $woocommerce, $post;
$args = array(
'post_id' => $post->ID,
'approve' => 'approve',
'type' => 'order_note'
);
$notes = get_comments( $args );
echo '<h2>' . __( 'Order Notes', 'woocommerce' ) . '</h2>';
echo '<ul class="order_notes">';
if ( $notes ) {
foreach( $notes as $note ) {
$note_classes = get_comment_meta( $note->comment_ID, 'is_customer_note', true ) ? array( 'customer-note', 'note' ) : array( 'note' );
?>
<li rel="comment_ID ) ; ?>" class="">
<div class="note_content">
comment_content ) ) ); ?>
</div>
<p class="meta">
comment_date_gmt ), current_time( 'timestamp', 1 ) ) ); ?>
</p>
</li>
<?php
}
} else {
echo '<li>' . __( 'There are no notes for this order yet.', 'woocommerce' ) . '</li>';
}
echo '</ul>';
}
As you can see this code is pretty simple, nothing really complex, so you can easily customize it. Please note the use of some nice functions like human_time_diff(), wptexturize(), or wp_kses_post().
Well, I hope you enjoyed that post, please leave a comment in the form below, I’d love to get your feedback about it!
This is nice, and fortunately you have the correct code on github (https://gist.github.com/corsonr/8560038) …but on this page the code seems to be messed up.
Thanks Remi and guy above!
It’s not
it’s
Dear Remi, unfortunately I can’t see a different way of contacting you. Having seen your great woo snippets I wonder wether you have a simple idea how to restrict a global addon to only one of 3 variations. And maybe you even know how to integrate the multiple shipping adresses of one order into the according invoice/ packing slip.
thx in advance and sorry for posting here
Clarissa