Posted on

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 code?

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 delicate files - if you need more guidance, please take a look at my free WooCommerce Customization video tutorial.

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?

Check out these free videos, tutorials and tips!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP
  • woocommerce-customize-shop-page-PHP
  • woocommerce-advanced-customization
  • how-to-edit-woocommerce-cart
  • woocommerce-customize-checkout-page-PHP
  • woocommerce-email-customization
  • woocommerce-conditional-logic

Rodolfo Melogli

Author, WooCommerce expert, WordCamp speaker and Internet marketer, Rodolfo Melogli has worked as a WooCommerce freelancer since 2011. He helps entrepreneurs and developers overcome their WooCommerce nightmares :) Rodolfo is the organiser of WordCamp Dublin, the Dublin WooCommerce Meetup, the Dublin Ecommerce Meetup and the Dublin WordPress Meetup. He enjoys interacting with people, travelling and chasing tennis & soccer balls. Of course, he loves pizza too.

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

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

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

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

    1. Excellent πŸ™‚

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

    1. 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 πŸ™‚

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

  6. 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 πŸ™‚

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

  8. How can I remove it from checkout page?

    1. 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 πŸ™‚

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

  10. 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 πŸ™‚

  11. Another cool snippet thanks and keep up the good work πŸ˜‰

  12. Great! Thanks

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

    1. Thank you Deborah πŸ™‚

Questions? Feedback? Support? Leave your Comment Now!
If you're writing code, please wrap it between: [php] code_here [/php]

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.