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.
PHP Snippet: Check if Product Category is inside the Cart – WooCommerce
/**
* @snippet Check if Product Category is in the Cart - WooCommerce
* @how-to businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @compatible WooCommerce 5
* @community https://businessbloomer.com/club/
*/
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...
// ..........
}
}
Thanks for the code!
Any idea how can we add multiple categories instead of only 1?
Many Thanks
Hello Irvin, has_term() also allows for an array of categories, so you could do this:
Hello, congratulations for the site, always very useful.
I wanted to make a change to your code by placing a free product according to the category.
I tried this but it doesn’t work:
Thanks
Your code has many errors, sorry but that’s custom troubleshooting work
Still works, is it possible to make it work on checkout page too ?
Thanks awesome snippet
Thank you. This works on the Checkout page as well
Is this still working with Woo 3.9.2?
Yep. Got issues?
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?
Just use the PHP “Not” operator: https://www.php.net/manual/en/language.operators.logical.php
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.
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!
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.
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
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.
I have Woo 3.2.2
Not sure Vuster, it works perfectly on my test website on Woo 3.2.2 🙂