WooCommerce: Display Categories Under Product Name @ Cart

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        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=72844
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 3.2.1
 */

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

function bbloomer_cart_item_category( $name, $cart_item, $cart_item_key ) {

$product_item = $cart_item['data'];

// make sure to get parent product if variation
if ( $product_item->is_type( 'variation' ) ) {
$product_item = wc_get_product( $product_item->get_parent_id() );
} 

$cat_ids = $product_item->get_category_ids();

// if product has categories, concatenate cart item name with them
if ( $cat_ids ) $name .= '</br>' . wc_get_product_category_list( $product_item->get_id(), ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', count( $cat_ids ), 'woocommerce' ) . ' ', '</span>' );

return $name;

}

Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7+.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce Customization?

Check out these free video tutorials!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP

Rodolfo Melogli

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as a WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

28 thoughts on “WooCommerce: Display Categories Under Product Name @ Cart

  1. 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

    add_filter( 'woocommerce_after_shipping_calculator', 'bbloomer_cart_item_category', 99, 3);

    but nothing happened. Category name is still auto gone after the page is loaded and the cart refreshed.
    Any ideas? 🙂

    1. 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

  2. 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

    1. 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

  3. Great tips, very usefull!

    How to show only the parent categories (or hide the subcategories)?

    I hope you can help me.
    Thanks

    1. 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

  4. GREAT snippet! Saved the day (and many hours!) for me. Thank you!

  5. 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).

  6. 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

    1. 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

  7. 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

    1. Hey Levi, you’re 100% right, this wasn’t working for single variations! Snippet is now fixed, test it out and let me know 🙂

  8. Thanks Rodolpho. Very useful.
    But how to remove link to category and leave category name?

    1. 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

  9. How can I remove it from checkout page?

  10. 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.

    1. 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

  11. 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…

    1. David, thanks for your comment! I guess that depends on the user experience and your specific website objectives 🙂

  12. Another cool snippet thanks and keep up the good work 😉

  13. Great! Thanks

  14. 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.

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between: [php]code_here[/php]. Failure to complying with this (as well as going off topic) will result in comment deletion. You should expect a reply in about a week - this is a popular blog but I need to get paid work done first. Please consider joining #BloomerArmada to ask me 1-to-1 WooCommerce questions. Thank you :)

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.