WooCommerce Order Notes in Completed Emails from WordPress Store

If you’ve ever made an order online, you know how important it is to have access to information about the status of your order or any other relevant data. By default, WooCommerce stores every action related to an order in its database, which is available in the administration sidebar when you edit an order. However, customers don’t have access to this information. The good news is that you can easily add this information to any email sent by WooCommerce.

In this post, we’ll show you how to add order notes to the complete email (the email sent when the order status is marked as completed), but you can use this method to add order notes to any other emails as well.

Custom Templates or Hooks?

Before we dive into the details of adding order notes to emails, let’s take a quick look at custom templates and hooks. All emails within WooCommerce are provided using templates, and you can override default templates by creating your own templates. A custom template allows you to override WooCommerce default files and use your own custom files instead. You can add order notes within a custom template, but we’ll show you another way to do it, using a hook.

Order Notes are WordPress Comments

Order notes are WordPress comments with 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 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 ‘

‘ . __( ‘Order Notes’, ‘woocommerce’ ) . ‘

‘;

echo ‘

    ‘;

    if ( $notes ) {

    foreach( $notes as $note ) {

    $note_classes = get_comment_meta( $note->comment_ID, ‘is_customer_note’, true ) ? array( ‘customer-note’, ‘note’ ) : array( ‘note’ );

    ?>

  • comment_content ) ); ?>

comment_ID ), human_time_diff( get_comment_date( ‘U’, $note->comment_ID ), current_time( ‘timestamp’, 1 ) ) ); ?>

}

} else {

echo ‘

  • ‘ . __( ‘There are no notes for this order yet.’, ‘woocommerce’ ) . ‘
  • ‘;

    }

    echo ‘

    ‘;

    }

    “`

    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().

    Conclusion

    Adding order notes to emails is a great way to keep your customers informed about their orders. With this simple code, you can easily add order notes to any email sent by WooCommerce. We hope you found this post helpful. If you have any questions or feedback, please leave a comment below.

    Stay in Touch

    spot_img

    Related Articles