How to Add category name in WooCommerce order emails

On a previous article, We explained how to display the product category in the WooCommerce -> Orders screen of WordPress dashboard. Here is another case that WooCommerce does not by default display the product category: the email you receive after a user has created a new order.

As you can see in the screenshot, this email sends you information about the product name, the quantity ordered and the product price:

WooCommerce orders email screenshot

Here is how to add the product category in the email information: In our theme’s functions.php, let’s add the following code:

function modfuel_woocommerce_before_order_add_cat($name, $item){

   $product_id = $item['product_id'];

   $_product = wc_get_product( $product_id );
   $htmlStr = "";
   $cats = "";
   $terms = get_the_terms( $product_id, 'product_cat' );

   $count = 0;
   foreach ( $terms as $term) {
    $count++;

    if($count > 1){
      $cats .= $term->name;
    }
    else{
      $cats .= $term->name . ',';
    }

   }

   $cats = rtrim($cats,',');

   $htmlStr .= $_product->get_title();

   $htmlStr .= "<p>Category: " . $cats . "</p>";

   return $htmlStr;
}

add_filter('woocommerce_order_item_name','modfuel_woocommerce_before_order_add_cat', 10, 2);

Then, we send a test order to verify that the product category is displayed:

WooCommerce orders email displaying item category screenshot

As always, before editing any WordPress file, make sure to take a backup first.

Waqas

I hope you enjoy reading this blog post. If you want my team to do WooCommerce Maintenance for you, click here.

Leave a Comment

Your email address will not be published. Required fields are marked *