×

 

Home Contact Us Payments SC Commerce SC Commerce Docs SC Commerce Snippets

Simply Computing

We make websites. Simple.

Display categories under product name in cart

This snippet adds an extra line in the cart under product name, to show the category or categories.

		/**
 * Display categories under product name in cart
 */

// Adds product category or categories in a line under the product name in the cart
// https://www.businessbloomer.com/woocommerce-display-categories-product-name-cart/

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

function scc_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 Raw Code ID: 93480