Here’s the story: I’m working with one of my freelance clients and I need to show certain content in the Checkout (a product-specific “Terms and Conditions”) if such product is in the Cart.
Now, I’ve always looked for products in the Cart by “looping” through the Cart with a foreach (here, for example: Apply a Coupon Programmatically if a Product is in the Cart). But as I said, after some random research, I found out about another magic WooCommerce function: “find_product_in_cart()“. Which means finding a product in the Cart doesn’t require custom loops or complex PHP… it’s just a “one liner”. Enjoy!
PHP Snippet: Easily Check if Product ID is Contained in the Cart – WooCommerce
Note: I believe this method only works with “simple” products. In case, use the snippet alternative you find below.
/**
* @snippet Check if Product ID is in the Cart - WooCommerce
* @how-to Get CustomizeWoo.com FREE
* @author Rodolfo Melogli
* @testedwith WooCommerce 3.9
* @community https://businessbloomer.com/club/
*/
add_action( 'woocommerce_before_cart', 'bbloomer_find_product_in_cart' );
function bbloomer_find_product_in_cart() {
$product_id = 282;
$product_cart_id = WC()->cart->generate_cart_id( $product_id );
$in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
if ( $in_cart ) {
$notice = 'Product ID ' . $product_id . ' is in the Cart!';
wc_print_notice( $notice, 'notice' );
}
}
PHP Snippet Alternative (the “old” way, still works): Check if Product ID is in the Cart Via a Foreach Loop – WooCommerce
/**
* @snippet Check if Product ID is in the Cart (Alternative) - WooCommerce
* @how-to Get CustomizeWoo.com FREE
* @author Rodolfo Melogli
* @testedwith WooCommerce 3.9
* @community https://businessbloomer.com/club/
*/
add_action( 'woocommerce_before_cart', 'bbloomer_find_product_in_cart_alt' );
function bbloomer_find_product_in_cart_alt() {
$product_id = 282;
$in_cart = false;
foreach( WC()->cart->get_cart() as $cart_item ) {
$product_in_cart = $cart_item['product_id'];
if ( $product_in_cart === $product_id ) $in_cart = true;
}
if ( $in_cart ) {
$notice = 'Product ID ' . $product_id . ' is in the Cart!';
wc_print_notice( $notice, 'notice' );
}
}
Hi!
How can I display an alternative message if the product has no been added to the cart?
I tried the following code but it doesn’t work (I’m not a developer ;D).
Hello Maina, that should definitely work – as long as your cart is not empty.
p.s. I never delete questions that are relevant, such as yours.
Hi Rodolfo, I used the alternative snippet – it seems that the first one indeed does not work with variable products. Client uses WordPress 5.5 + Divi 4.6.0 + PHP 7.3 and all works perfectly. Thank you.
Thank you! First snippet will return Variation ID and not Product ID if product is variable I think
How to show the message in the Checkout page instead?
https://www.businessbloomer.com/woocommerce-display-weight-cart-checkout/
What about:
and you can call it
Beautifully working π
Whatever works for you π
Hi there Rodolfo,
I’ve learnt so much from all your previous posts. Many thanks.
I am now trying to get the post ID for a variation product from the
object. Basically I have data in the custom fields which I need to pull into the cart item. It works for other products because the product ID seems to be the same as the post ID. but for variation products, it pulls the variation ID instead.
Do you know of the best way to get the post ID for variation product within the cart?
Many thanks in advance
Not to worry. I figured it out : )
Well done!
In order to check if a specific product id is in the cart (both for simple and variable products) you can use this:
It’s not working for variations.
Ok thanks
@Alessandro,
I had the same problem for multiple products. I came with this snippet:
The snippet it doesn’t work with latest woocommerce version. Only the old way works now.
Tried on latest woocommerce with astra theme and Code Snippets plugin. No other plugin enabled.
Hey Michael, thanks for your comment! Have you tried the “old way” snippet as well? Did that work?
Thank you for this snippet! I’m trying to do some complex cart / order manipulations and these code snippets are really useful.
Excellent, thank you so much Anca!
Hey there,
I came across this blog which is excellent btw to try to implement something similar where I am trying to add an extra step on checkout when product id 3255 is begin select but I keep getting the follow fatal error –> Fatal error: Call to a member function generate_cart_id() on a non-object. It is complaining about the following line –> $product_cart_id = WC()->cart->generate_cart_id( $product_id );
Any idea?
Thank you
Hello Paul, 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
Hi Rodolfo!
Is it possible to treat $product_id as an array and check for multiple ids all at once?
I’d like to check if at least one product of a list is in the cart in orde to add another product as a free gift.
What do you think about?
Thanks in advance for your reply.
Hey Alessandro, thanks so much for your comment! Yes, you can look for multiple “needles” in a PHP array: https://stackoverflow.com/questions/7542694/in-array-multiple-values
Hope this helps!
Wonder if there’s something similar for product category?
Hey Vuster, thanks for your comment! You could get a list of IDs inside your category, and then use this π
Love WooCommerce ….. most of the time π
So simple to work with.
find_product_in_cart() was new to me … I will test it -Thanks
Glad this helped π