Menu Close

WooCommerce: Display Product Categories @ Cart & Checkout Pages

default

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 share with you how to display product categories in the Cart and Checkout? This adds a nice touch to those two vital pages, and prints a list of product categories under each cart item.

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 Product Categories @ WooCommerce Cart, Checkout
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 7
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

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

function bbloomer_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;

}
View Source
Posted in WooCommerce Tips