We’ve covered how to check if product is in the WooCommerce cart and if certain product category is in the cart. In this article we’ll cover how to check if the WooCommerce order contains a product or product category.

As a store owner you might want to show specific content to the customer when he orders something. After the order is placed, the customer is then redirected to a thank you page. There you might want to show a specific message for a specific product in the order. Very often you want to track paid conversions and add a conversion event for Google Ads or Facebook ads.

It is very simple to check if particular product is in the order in WooCommerce and there is no need to modify any WooCommerce template file. All you need to do is go to your child theme’s functions.php and place the following php snippet:

add_action( 'woocommerce_thankyou', 'name_your_function_check_order_product_id');
   
function name_your_function_check_order_product_id( $order_id ){
$order = wc_get_order( $order_id );
$items = $order->get_items(); 
foreach ( $items as $item_id => $item ) {
   $product_id = $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id();
      if ( $product_id == 12345 ) {
        // place your code, copy or else here
      }
}
}

Remember to replace “12345” from the snippet with your Product id in WooCommerce.

How to get Product id in WooCommerce?

Go ahead and edit a product in WooCommerce. Now take a look at the url, it will look like:

https://www.example.com/wp-admin/post.php?post=12345&action=edit

You will find the product id in ?post=12345.

You can also check if several products are present in the order

add_action( 'woocommerce_thankyou', 'name_your_function_check_order_product_id');
   
function name_your_function_check_order_product_id( $order_id ){
$order = wc_get_order( $order_id );
$items = $order->get_items(); 
foreach ( $items as $item_id => $item ) {
   $product_id = $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id();
      if ( $product_id == 12345 &&  $product_id == 56789 ) {
        // Both products have to be in the order. 
        // place your code, copy or else here
      }
  	if ( $product_id == 12345 ||  $product_id == 56789 ) {
      // Any of the products have to be in the order  
      // place your code, copy or else here
      }
}
}

You can use this function not only to the WooCommerce Thank you page, but anywhere you want, for example in the email for completed order to the customer. Find all WooCommerce hooks and actions at the official documentation here.

// replace this:
add_action( 'woocommerce_thankyou', 'name_your_function_check_order_product_id');

//with this:
add_action( 'woocommerce_email_before_order_table', 'name_your_function_check_order_product_id');

How to check if product category is in the order

Same thing as above, but we check for a whole category of products in WooCommerce order. Remember to change ‘YOUR-PRODUCT-CATEGORY’ with your product category.

add_action( 'woocommerce_thankyou', 'webroom_check_product_category_in_order', 5 );
 
function webroom_check_product_category_in_order( $order_id ) { 
 
if ( ! $order_id ) {
    return;
}
 
$order = wc_get_order( $order_id );

$category_in_order = false;

$items = $order->get_items(); 
    
foreach ( $items as $item ) {      
    $product_id = $item['product_id'];  
    if ( has_term( 'YOUR-PRODUCT-CATEGORY', 'product_cat', $product_id ) ) {
        $category_in_order = true;
        break;
    }
}
 
// Echo content only if $category_in_order == true
   
if ( $category_in_order ) {
    echo '<p>your custom content here</p>';     
}

}




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.