Home » WooCommerce: Display Categories Under Product Name @ Cart

WooCommerce: Display Categories Under Product Name @ Cart

by Tutor Aspire

While working for a freelance client I had to “detect” the cart item categories in order to apply some PHP customization. So I thought – why not sharing with you how to display product categories in the Cart? This adds a nice touch to the Cart page 🙂

Also, I’m glad to introduce you to the amazing world of “wc_get_product_category_list“, a very handy WooCommerce PHP function!

Display product categories under each item in the WooCommerce Cart

PHP Snippet: Display Categories Under Product Name @ WooCommerce Cart

/**
 * @snippet       Display Categories Under Product Name @ WooCommerce Cart
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire
 * @testedwith    WooCommerce 5.0
 * @donate $9     https://www.tutoraspire.com
 */

add_filter( 'woocommerce_cart_item_name', 'tutoraspire_cart_item_category', 9999, 3 );

function tutoraspire_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 .= '
' . wc_get_product_category_list( $product->get_id(), ', ', '' . _n( 'Category:', 'Categories:', count( $cat_ids ), 'woocommerce' ) . ' ', '' ); return $name; }

You may also like