WooCommerce: Check if Product Category is in the Cart

We already studied how to detect if a product ID is in the cart – but if you take a look at the comments many of you were asking how to detect product categories.

So, today we’ll do exactly that. You can disable shipping rates, payment gateways, you can print messages, you can apply coupon programmatically… there are lots of things you can do “conditionally”, based on whether a given product category is in the Cart or not.

Check if Product Category is in the Cart – WooCommerce

PHP Snippet: Check if Product Category is in the Cart – WooCommerce


/**
 * @snippet       Check if Product Category is in the Cart - WooCommerce
 * @how-to        Get CustomizeWoo.com FREE
 * @sourcecode    https://businessbloomer.com/?p=72900
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 3.1.2
 */

add_action('woocommerce_before_cart', 'bbloomer_check_category_in_cart');

function bbloomer_check_category_in_cart() {

// Set $cat_in_cart to false
$cat_in_cart = false;

// Loop through all products in the Cart        
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

    // If Cart has category "download", set $cat_in_cart to true
    if ( has_term( 'download', 'product_cat', $cart_item['product_id'] ) ) {
        $cat_in_cart = true;
        break;
    }
}
  
// Do something if category "download" is in the Cart      
if ( $cat_in_cart ) {

// For example, print a notice
wc_print_notice( 'Category Downloads is in the Cart!', 'notice' );

// Or maybe run your own function...
// ..........

}

}

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. You can start learning how to customize WooCommerce without unnecessary plugins. Watch me code and learn by example!

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

9 thoughts on “WooCommerce: Check if Product Category is in the Cart

  1. And on the contrary, as it would be if instead of indicating that “this category” should be in the cart, do you want to exclude a category?

  2. Hye Rodolfo Melogli,
    I am trying to create a functionality that if i add a product to cart and then after if i want to add a another product to cart having low price then previous product it will give me a warning msg in popup. Will you please help me regarding this?
    I am new to php and not getting any clue from anywhere.

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

  3. Code works fine!

    Few questions:

    1. What if I want to add 3 categories to the function? So a product has to be in one of the 3 categories.

    2. I want to combine the categories with a minimal subtotal that is in the cart.

    So basically I want the message to appear when:
    A product in one of the 3 specified categories is in the cart AND the subtotal is less than 699.

    Hope you can help me out.

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

  4. I used this code but nothing happens. If I change get_id() to just id then it works. Even though I know id is deprecated and Woo recommends using get_id()

    So, I changed this line
    if ( has_term( ‘download’, ‘product_cat’, $product->get_id() ) ) {
    to
    if ( has_term( ‘download’, ‘product_cat’, $product->id ) ) {

    Odd.

    1. I have Woo 3.2.2

      1. Not sure Vuster, it works perfectly on my test website on Woo 3.2.2 ๐Ÿ™‚

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 *