How to check if the WooCommerce cart contains a product or product category
We’ve covered how to check if product is in the WooCommerce order and if certain category is in the order. In this article we’ll cover how to check if the WooCommerce cart contains a product or product category.
Use case
Checking if a product or product category is present in the customer’s cart in WooCommerce is very useful if you want to make custom upsell or show the customer custom message for the specific product/product category when in cart. Other use case is if you want to show different payment method for a specific product/product category or offer free shipping.
PHP Snippet: How to check if product id is in WooCommerce cart
add_action( 'woocommerce_checkout_before_customer_details', 'webroom_check_if_product_in_cart' ); function webroom_check_if_product_in_cart() { $product_id = 1493; // CHANGE THIS WITH YOUR PRODUCT ID $product_cart_id = WC()->cart->generate_cart_id( $product_id ); $in_cart = WC()->cart->find_product_in_cart( $product_cart_id ); if ( $in_cart ) { echo 'YOUR CONTENT HERE'; } }
How to get Product id in WooCommerce
Go ahead and edit a product. Now take a look at the url, it will look like:
https://www.example.com/wp-admin/post.php?post=1493&action=edit
You will find the product id in ?post=1493.
PHP Snippet: How to check if product category is in WooCommerce cart
add_action( 'woocommerce_checkout_before_customer_details', 'webroom_check_if_product_category_is_in_cart' ); function webroom_check_if_product_category_is_in_cart() { $cat_in_cart = false; foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { if ( has_term( 'YOUR-PRODUCT-CATEGORY', 'product_cat', $cart_item['product_id'] ) ) { $cat_in_cart = true; break; } } // Do something if the category is in the Cart if ( $cat_in_cart ) { echo 'YOUR CUSTOM CONTENT'; } }
Please note you have to change the add_action hook ‘woocommerce_checkout_before_customer_details’ with the one you want the content to appear. Below is a list with WooCommerce checkout hooks.
The WooCommerce Checkout Page has the following hooks:
- woocommerce_before_checkout_form
- woocommerce_checkout_before_customer_details
- woocommerce_checkout_billing
- woocommerce_before_checkout_billing_form
- woocommerce_after_checkout_billing_form
- woocommerce_before_checkout_registration_form
- woocommerce_after_checkout_registration_form
- woocommerce_checkout_shipping
- woocommerce_before_checkout_shipping_form
- woocommerce_after_checkout_shipping_form
- woocommerce_before_order_notes
- woocommerce_after_order_notes
- woocommerce_checkout_after_customer_details
- woocommerce_checkout_before_order_review_heading
- woocommerce_checkout_order_review
- woocommerce_checkout_before_order_review
- woocommerce_review_order_before_cart_contents
- woocommerce_review_order_after_cart_contents
- woocommerce_review_order_before_shipping
- woocommerce_review_order_after_shipping
- woocommerce_review_order_before_order_total
- woocommerce_review_order_after_order_total
- woocommerce_review_order_before_payment
- woocommerce_review_order_before_submit
- woocommerce_review_order_after_submit
- woocommerce_review_order_after_payment
- woocommerce_checkout_after_order_review
- woocommerce_after_checkout_form
Learn more about WooCommerce Checkout Hooks with this visual guide.
Related Articles
If you enjoyed reading this, then please explore our other articles below:
More Articles
If you enjoyed reading this, then please explore our other articles below: