How to check if a product is in a WooCommerce order

check if a product is in a WooCommerce order

If you are a WordPress/ WooCommerce developer or business owner and looking for a method to check whether the product is in order or not using a product id or product category is in the order. Then this article is most important for you.

Check if a product id is in a WooCommerce order

To check if a product is in a WooCommerce order, you can use the WC_Order class and its get_items() method to retrieve all items in the order, and then loop through them to check if the product you’re looking for is among them.

Suggested Read: WooCommerce: Display Product Discount After Coupon Applied in Cart Summary @ Checkout

Here’s an example code snippet:

In this code, you first set the $order_id and $product_id variables to the order and product IDs you want to check. Then, you use the wc_get_order() function to retrieve the order object associated with the given ID.

Once you have the order object, you can use the get_items() function to retrieve all items in the order. You then loop through each item and use the get_product_id() function to check if the current item matches the product ID you’re looking for.

If a match is found, the code outputs a message saying that the product was found in the order. Otherwise, nothing is output.

Check if a product is in the order on Thank you page

You can use the above function on the WooCommerce thank you page. To do this, you have to use the woocommerce hook add_action( ‘woocommerce_thankyou’, ‘tutorialswebsite_check_order_product_id’) in your theme functions.php file.

Here’s an example code snippet:

Are you want to get implementation help, or modify or extend the functionality of this script?

A Tutorialswebsite Expert can do it for you.

I hope you found this article helpful for your WooCommerce project. Let’s see the next point to check if the product category in the WooCommerce order.

Also Read: Create WooCommerce custom product type programmatically

Check if the product category is in the order

To check if a product category is in a WooCommerce order, you can modify the code I provided earlier to retrieve the product categories for each item in the order, and then check if the category you’re looking for is among them.

Here’s an example code snippet:

This code is similar to the previous example, but instead of checking for a specific product ID, we’re retrieving the category IDs for each product using get_category_ids() and checking if $category_slug is among them using in_array().

If the loop completes without finding the category, then it is not in the order.

Wrapping Words

Thanks for reading 🙏, I hope you found the How to check if a product is in a WooCommerce order tutorial helpful for your project. Keep learning! If you face any problems – I am here to solve your problems.

Related posts

One Thought to “How to check if a product is in a WooCommerce order”

  1. […] How to check if a product is in a WooCommerce order […]

Comments are closed.