WooCommerce: Display Product Categories @ Cart & Checkout Pages

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
 * @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;

}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: How to Fix the “Cart is Empty” Issue
    For some reason, sometimes you add products to cart but the cart page stays empty (even if you can clearly see the cart widget has products in it for example). But don’t worry – it may just be a simple cache issue (and if you don’t know what cache is that’s no problem either) or […]
  • WooCommerce: “You Only Need $$$ to Get Free Shipping!” @ Cart
    This is a very cool snippet that many of you should use to increase your average order value. Ecommerce customers who are near the “free shipping” threshold will try to add more products to the cart in order to qualify for free shipping. It’s pure psychology. Here’s how we show a simple message on the […]
  • WooCommerce: Cart and Checkout on the Same Page
    This is your ultimate guide – complete with shortcodes, snippets and workarounds – to completely skip the Cart page and have both cart table and checkout form on the same (Checkout) page. But first… why’d you want to do this? Well, if you sell high ticket products (i.e. on average, you sell no more than […]
  • WooCommerce: Disable Payment Method If Product Category @ Cart
    Today we take a look at the WooCommerce Checkout and specifically at how to disable a payment gateway (e.g. PayPal) if a specific product category is in the Cart. There are two tasks to code in this case: (1) based on all the products in the Cart, calculate the list of product categories in the […]
  • WooCommerce: Add Privacy Policy Checkbox @ Checkout
    Here’s a snippet regarding the checkout page. If you’ve been affected by GDPR, you will know you now need users to give you Privacy Policy consent. Or, you might need customer to acknowledge special shipping requirements for example. So, how do we display an additional tick box on the Checkout page (together with the existing […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent 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. Follow @rmelogli

39 thoughts on “WooCommerce: Display Product Categories @ Cart & Checkout Pages

  1. Thanks for the code! Is it also possible to use tags instead of categories?

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

  2. Hello Rodolfo, would it be possible to remove the “Category:” text, hence just showing the category title?
    Thank you for your lovely snippets.

    1. Of course!

      Try removing this part:

      . _n( 'Category:', 'Categories:', count( $cat_ids ), 'woocommerce' ) . ' '
  3. Great Work! Thanks a lot!!

    Its there a way to exclude a category?

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

  4. working great! thank you!

  5. Your code includes a typo

    if ( $cat_ids ) $name .= ''

    it should be or or

    1. The typo got removed. You have a closing slash in front of your br tag. It should be omitted or after the br tag.

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

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

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

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

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

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

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

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

  14. How can I remove it from checkout page?

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

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

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

  18. Great! Thanks

  19. 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? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

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