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!

PHP Snippet: Display Categories Under Product Name @ WooCommerce Cart
/**
* @snippet Display Product Categories @ WooCommerce Cart, Checkout
* @how-to businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @testedwith WooCommerce 7
* @community https://businessbloomer.com/club/
*/
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;
}
Thanks for the code! Is it also possible to use tags instead of categories?
Jelte, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hello Rodolfo, would it be possible to remove the “Category:” text, hence just showing the category title?
Thank you for your lovely snippets.
Of course!
Try removing this part:
Great Work! Thanks a lot!!
Its there a way to exclude a category?
Hello Hans thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
working great! thank you!
Nice!
Your code includes a typo
it should be or or
The typo got removed. You have a closing slash in front of your br tag. It should be omitted or after the br tag.
Thank you!
I have try some things to fix the problem that i have desribed in the previous comment. That your code works in my website, in the checkout page i get to see the category name in the cart, but after some seconds, when the shipping calculation is over and the cart is refreshed, the category name is gone.
So, i have try to add a filter this filter
but nothing happened. Category name is still auto gone after the page is loaded and the cart refreshed.
Any ideas? 🙂
Hey Christoforos, thanks so much for your comment! I just retested this on the latest version of WooCommerce and it still works. Unfortunately this looks like custom troubleshooting work and I cannot help here via the blog comments. Thanks a lot for your understanding! ~R
Hey Rodolfo – great snippet, thanks!
Is it possible to remove the link from the displayed product category? So only display the text and not make it clickable?
Thanks,
Giuls
Hello Giuls – thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R
Great tips, very usefull!
How to show only the parent categories (or hide the subcategories)?
I hope you can help me.
Thanks
Ciao Fabio, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R
GREAT snippet! Saved the day (and many hours!) for me. Thank you!
Excellent 🙂
Hello Rodolf,
I want to display the category on the shop page under each product.
Can you help me with a function?
I want the same display but not on the cart, just on the shop page.
The products are display like this:
Image
Title
Price
Category( i want).
Hey George, thanks for your comment! I suggest you take a look at “conditional logic”: https://businessbloomer.com/conditional-logic-woocommerce-tutorial/ and https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Let me know 🙂
Hello Guys,
Above code working nicely on our site. But i want to show product category name above product name. Please let me know any needed changes applying here.
Thanks
Hey Divyesh, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R
I am a bit perplexed.
This snippet doesn’t work for all products in the same category.
Could it be the variation, the name? I have tried to clear cache as well.
The parent category is : Tents and Event Products
Examples:
Prize Putt Kit
Category: Prize Putt
Deluxe 10′ X 10′ Event Tent Kit (Full-Color Imprint, 1 Location) – Black – PMS Black
Hey Levi, you’re 100% right, this wasn’t working for single variations! Snippet is now fixed, test it out and let me know 🙂
Thanks Rodolpho. Very useful.
But how to remove link to category and leave category name?
Hi Cris, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R
How can I remove it from checkout page?
Hey Bulent, I already replied to you. I suggest you take a look at “conditional logic”: https://businessbloomer.com/conditional-logic-woocommerce-tutorial/ and https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Let me know 🙂
Hello,
Thank you for all these hard work. It really helps those of us new to woocommerce. How do I display the variations of a product under the product name in the cart? So if a product comes in different colours, or sizes, and a user add to cart, how to display that variation?
Thank you.
Akos, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R
Code work for me. Storefront theme. Thank you! But I am asking myself a question: Can’t this information make the customer distracted? I am just curious since I know when it comes to the cart page the only thing we really want is the customer to proceed to checkout…
David, thanks for your comment! I guess that depends on the user experience and your specific website objectives 🙂
Another cool snippet thanks and keep up the good work 😉
Cheers!
Great! Thanks
Awesome 🙂
Thank you for all the very useful information. Although I am very new to Woocommerce, I love to read and watch your information. It inspires me to think how to provide more info on my site.
Thank you Deborah 🙂