Are you looking for how to customize WooCommerce order emails for specific products or product categories? You have a WooCommerce store with great products. All of these products are using the general WooCommerce order notification email, right?. That is good but those notification emails may not be well enough for all of your products. Some of your products or product categories may need a customized email notification to your customers when they purchase those from your store.

The good news is that you can do this with just a few bits of code. In this tutorial, we are going to discuss this topic in an easy and simplest way.

Scenario 01: Customize WooCommerce order emails for a specific product category

Let’s consider a real-life example. We have some products (food items) in our WooCommerce store with the below product categories:

  • Prepared Meals (with category slug: prepared-meals)
  • Frozen Meals (with category slug: frozen-meals)
  • Fresh Vegetables (with category slug: fresh-vegetables)

When a customer purchases food from any of these product categories, he will get an order confirmation email like the below screenshot

What actions do you have to take if you want to customize this email only for the “Prepared Meals” product category? Say you want to add the below message to this order confirmation email?

Our prepared meals are the best in the city. If you want to learn how to cook these meals, please visit our cookie recipe plan from here

All you need to do is open your theme’s functions.php file and copy/paste the below code snippet. Modify as per your own need and save and re-upload the file to your server.

// customize woocommerce order emails based on product category
add_action( 'woocommerce_email_before_order_table', 'cdxn_custom_order_emails_product_category', 999 );

function cdxn_custom_order_emails_product_category( $order_id ) {
    // message to include in the order confirmation email
    $message ='Our prepared meals are the best in the city. If you want to learn how to cook these meals, please visit our <a href="#">cookie recipe plan from here</a><br><br>';
    $order = wc_get_order( $order_id );
    $cat_in_order = false;
    $items = $order->get_items();
        
    foreach ( $items as $item ) {      
        $product_id = $item->get_product_id();  
        if ( has_term( 'prepared-meals', 'product_cat', $product_id ) ) {
            $cat_in_order = true;
            break;
        }
    }
    
    if ( $cat_in_order ) {
        echo $message;
    }
}

Congrats! You have made it. Whenever a customer purchases food from the ‘Prepared Meals’ category, he will have those extra bits of messages in the order confirmation email. Here is a screenshot:

Scenario 02: Customize WooCommerce order emails for specific product IDs

That’s a nice question. Sometimes, you may want to customize the email for some specific products or for a single product. In that case, we just need to adjust the code snippet a little bit. We are going to use the products IDs instead of product categories like below:

// customize woocommerce order emails based on product IDs
add_action( 'woocommerce_email_before_order_table', 'cdxn_custom_order_emails_product_ids', 999 );

function cdxn_custom_order_emails_product_ids( $order_id ) {
    // Product IDs for which you want to send the custom message.
    $product_array = array(10, 11, 12, 13, 14, 15);
    // message to include in the order confirmation email
    $message ='Our prepared meals are the best in the city. If you want to learn how to cook these meals, please visit our <a href="#">cookie recipe plan from here</a><br><br>';
    $order = wc_get_order( $order_id );
    $product_in_order = false;
    $items = $order->get_items();
        
    foreach ( $items as $item ) {      
        $product_id = $item->get_product_id();  
        if ( in_array($product_id, $product_array) ) {
            $product_in_order = true;
            break;
        }
    }
    
    if ( $product_in_order ) {
        echo $message;
    }
}

You can use HTML tags inside the additional message.

A little explanation

Now as you know, how to customize WooCommerce order emails for specific products, we are going to have an overview of the above code snippets. We have created our custom functions which print/echo an additional message based on the product category slug and product ID. After that, we have hooked those functions into the WooCommerce woocommerce_email_before_order_table action hook.

Wait, if we want to change the position of that additional message, how can we do that?

The answer is simple. You just need to hook those functions to another action hook that is available for the WooCommerce email notifications.

We hope this tutorial was helpful and easy for you. Here are some more WooCommerce tutorials that may interest or help with your store! You are also welcome to visit our WordPress blog page from here.

Thank you,