Display Categories Under Product Name on Cart

To add a nice touch to the Cart page Why not display product categories in the Cart. This can be Done using wc_get_product_category_list“, a WooCommerce PHP function! You can add the following Snippet to your Functions.php.

add_filter( 'woocommerce_cart_item_name', 'codeithub_cart_item_category', 9999, 3 );
 
function codeithub_cart_item_category( $name, $cart_item, $cart_item_key ) {
 
   $product = $cart_item['data'];
   if ( $product->is_type( 'variation' ) ) {
      $product = wc_get_product( $product->get_parent_id() );
   }
 
   $cat_ids = $product->get_category_ids();
 
   if ( $cat_ids ) $name .= '<br>' . wc_get_product_category_list( $product->get_id(), ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', count( $cat_ids ), 'woocommerce' ) . ' ', '</span>' );
 
   return $name;
 
}

Before:

See also  Display “product already in cart” instead of “add to cart” button
Default Cart Page

After:

Show Category on Cart

Leave a Reply

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