How to Add category name in WooCommerce order emails

When user has created a new order and receive email it wil show all product detail but not display product category. So how to add category name in order detail page.

So, here is my solution to display category name in order emails, Add below line of code in your theme’s function.php of your active child theme (or theme) and Save the file.

function  woocommerce_add_category_order_emails ($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 .= "

Category: " . $cats . "

"; return $htmlStr; } add_filter('woocommerce_order_item_name','woocommerce_add_category_order_emails', 10, 2);
Scroll to Top