Check if Product is in Cart

In this simple guide I will show how to check in WooCommerce if a product with specific ID is in cart programmatically. We are also going to do it for different types of products and take a look at products with custom cart item data.

Let’s go.

Simple Products

For simple and external products without any cart item data it is dead simple.

$product_id = 12345;

if( WC()->cart->find_product_in_cart( WC()->cart->generate_cart_id( $product_id ) ) ) {
	// Yes, it is in cart!
}
  • Works for simple products,
  • Doesn’t work for variable products,
  • Doesn’t work for variations,
  • Doesn’t work for simple products if there are some cart item data provided.

There is also another way where are also going to check if there is an array element with a specific product_id column inside WC()->cart->get_cart().

$product_id = 12345;

if( in_array( $product_id, array_column( WC()->cart->get_cart(), 'product_id' ) ) ) {
	// Yes, it is in cart!
}

This method is considered obsolete, but.

Let’s make our examples a little bit more practical by displaying a message inside woocommerce_before_cart action hook.

add_action( 'woocommerce_before_cart', 'misha_if_product_in_cart' );

function misha_if_product_in_cart() {

	$product_id = 12345;

	if( WC()->cart->find_product_in_cart( WC()->cart->generate_cart_id( $product_id ) ) ) {
		wc_print_notice( 
			sprintf( 'Product ID %d is in the Cart!', $product_id ), 
			'notice'
		);
	}

}
Check if product is in cart WooCommerce
Just displaying this message before the cart items in Storefront theme.

Products with custom cart item data

Sometimes your products (of any type) can be added to cart with some additional data, it is described in details in this example. If so, method WC()->cart->generate_cart_id() is not going to work as expected.

What is cart item data? It could be anything, for example a custom field “Name on T-Shirt”.

custom cart item data of a product

Cart item data is usually registered as a pair of key and value. Just like post meta. And we have to pass that pair of key and value into WC()->cart->generate_cart_id() method. Like this:

$product_cart_id = WC()->cart->generate_cart_id( 
	$product_id, 
	0, 
	array(), 
	array( 'name-on-t-shirt' => 'Misha' ) 
);

if( WC()->cart->find_product_in_cart( $product_cart_id ) ) {
	// Yep, it is in the cart
}

Variable Products

Checking if a variable product is in the cart isn’t too difficult either. But it is good to know that there are two scenarios here – are we going to check if there is a specific variable product in the cart by its product ID or are we going to check if there is a specific variation in the cart by its variation ID?

Check if a variable product with any variation is in the cart

If it doesn’t even matter for us what product variation has been selected, then it comes down to the condition we already used for simple products before.

$variable_product_id = 12345;

if( in_array( $variable_product_id, array_column( WC()->cart->get_cart(), 'product_id' ) ) ) {
	// Yes, it is in cart!
}

Check if a variable product with a specific variation is in the cart

It becomes a little bit more difficult because we have to know three things:

  1. Product ID,
  2. Variation ID,
  3. Variation attribute slug and value.
$product_id = 12345;
$variation_id = 54321;
$variation = array( 'attribute_magical' => 'Yes' );
$cart_id = WC()->cart->generate_cart_id( $product_id, $variation_id, $variation );
	
if( WC()->cart->find_product_in_cart( $cart_id ) ) {
	// Yes, this variation is in the cart
}
Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on X