Better Code For WooCommerce Order Complete Email Notes
1 min read

Better Code For WooCommerce Order Complete Email Notes

Table of contents

I previously wrote a post to help you add code to WooCommerce Order Complete emails that would display the proper order notes(original article: Add Order Notes To WooCommerce Completed Order Email). This is an updated article with newer code that also includes some error checking.

Read that article to get a handle on things. I'll jump straight in here to show you the code here and then give a quick code explanation.

Why Use This Better Code

This code first checks to see if there is anything to actually print first, and only if there are order notes to print will it even display the heading. In the old code, the heading would display whether you had order notes to print or not. I've also cleaned up the code a little bit. Clean code is good code.

Better WooCommerce Order Complete Email Notes Code

get_customer_order_notes();
if($comments){
    echo '

' . __( 'Order Notes', 'woocommerce' ) . '

'; foreach($comments as $comment) { echo $comment->comment_content . '
'; } } ?>

Code Explanation

This gets the (proper) order notes and tests to see if there actually are any. IF there are order notes to display, only then do we go into the display code.

$comments = $order->get_customer_order_notes();
if($comments){

Here I've simplified the code and put it on less lines. Other than that, it's the same as before.

echo '

' . __( 'Order Notes', 'woocommerce' ) . '

'; foreach($comments as $comment) { echo $comment->comment_content . '
'; }