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:

  1. woocommerce_before_checkout_form
  2. woocommerce_checkout_before_customer_details
  3. woocommerce_checkout_billing
  4. woocommerce_before_checkout_billing_form
  5. woocommerce_after_checkout_billing_form
  6. woocommerce_before_checkout_registration_form
  7. woocommerce_after_checkout_registration_form
  8. woocommerce_checkout_shipping
  9. woocommerce_before_checkout_shipping_form
  10. woocommerce_after_checkout_shipping_form
  11. woocommerce_before_order_notes
  12. woocommerce_after_order_notes
  13. woocommerce_checkout_after_customer_details
  14. woocommerce_checkout_before_order_review_heading
  15. woocommerce_checkout_order_review
  16. woocommerce_checkout_before_order_review
  17. woocommerce_review_order_before_cart_contents
  18. woocommerce_review_order_after_cart_contents
  19. woocommerce_review_order_before_shipping
  20. woocommerce_review_order_after_shipping
  21. woocommerce_review_order_before_order_total
  22. woocommerce_review_order_after_order_total
  23. woocommerce_review_order_before_payment
  24. woocommerce_review_order_before_submit
  25. woocommerce_review_order_after_submit
  26. woocommerce_review_order_after_payment
  27. woocommerce_checkout_after_order_review
  28. woocommerce_after_checkout_form

Learn more about WooCommerce Checkout Hooks with this visual guide.




Recent Articles

Blog Categories

Related Articles

If you enjoyed reading this, then please explore our other articles below:

Comments

Leave a comment

Leave a Reply

Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.