Posted on

WooCommerce: Check if Product ID is in the Cart

This is one of those “OMG” moments ๐Ÿ™‚

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.

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 need to do custom loops or complex PHP… it’s a “one liner”!

WooCommerce: find if product ID is in the Cart

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

 

/**
 * @snippet       Check if Product ID is in the Cart - WooCommerce
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=72733
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 3.3.3
 */
 
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 – WooCommerce

 

/**
 * @snippet       Check if Product ID is in the Cart (Alternative) - WooCommerce
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=72733
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 3.3.3
 */
 
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' );

	}

}

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.

13 thoughts on “WooCommerce: Check if Product ID is in the Cart

  1. @Alessandro,

    I had the same problem for multiple products. I came with this snippet:

     
    $product_id = array(144,146,157,698,701);
     
    	foreach ($product_id as $product_id => $product) {
    	 
    	    $product_cart_id = WC()->cart->generate_cart_id( $product );
    	    $in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
    	 
    	    if ( $in_cart ) {
    	 
    			$notice = '<p>Let op: Levering voorjaar 2019 !!</p>';
    	        wc_print_notice( $notice, 'notice-delivery' );
     
        	}
        
    	}
    
    
  2. 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.

    1. Hey Michael, thanks for your comment! Have you tried the “old way” snippet as well? Did that work?

  3. Thank you for this snippet! I’m trying to do some complex cart / order manipulations and these code snippets are really useful.

    1. Excellent, thank you so much Anca!

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

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

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

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

  6. Wonder if there’s something similar for product category?

    1. Hey Vuster, thanks for your comment! You could get a list of IDs inside your category, and then use this ๐Ÿ™‚

  7. Love WooCommerce ….. most of the time ๐Ÿ™‚
    So simple to work with.
    find_product_in_cart() was new to me … I will test it -Thanks

    1. Glad this helped ๐Ÿ™‚

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.