I found this great snippet from this website
The following is the function to check if a specific product exists in cart:
function woo_in_cart($product_id) {
global $woocommerce;
foreach($woocommerce->cart->get_cart() as $key => $val ) {
$_product = $val['data'];
if($product_id == $_product->id ) {
return true;
}
}
return false;
}
And this to use anywhere needed:
if(woo_in_cart(123)) {
// Product is already in cart
}
The problem is how to use it to check multiple products like this:
if(woo_in_cart(123,124,125,126...)) {
// Product is already in cart
}
Thanks.
Here is a custom function with an argument that accepts a unique integer product ID or an array of product IDs, and that will return the number of matched Ids that are in cart.
The code handle any product type, including variable product and product variations:
This code goes in function.php file of your active child theme (active theme or in any plugin file).
Code is tested and works.
USAGE:
1) For a unique product ID (integer):
2) For an array of product IDs:
3) For an array of product IDs for 3 or more matched cart items for example: