Posted on

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        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @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 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.

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

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

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

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

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.