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:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
$order_id = 123; // Replace with the order ID you want to check $product_id = 456; // Replace with the product ID you want to check $order = wc_get_order( $order_id ); // Get the order object if ( $order ) { $items = $order->get_items(); // Get all items in the order foreach ( $items as $item ) { if ( $item->get_product_id() == $product_id ) { // Check if the current item is the product we're looking for echo 'Product found in order!'; break; } } } |
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:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
add_action( 'woocommerce_thankyou', 'tutorialswebsite_check_order_product_id' ); function tutorialswebsite_check_order_product_id( $order_id ){ $product_id = 456; // Replace with the product ID you want to check $order = wc_get_order( $order_id ); // Get the order object if ( $order ) { $items = $order->get_items(); // Get all items in the order foreach ( $items as $item ) { if ( $item->get_product_id() == $product_id ) { // Check if the current item is the product we're looking for echo 'Product found in order!'; break; } } } } |
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:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
$order_id = 123; // replace with the ID of the order you want to check $category_slug = 'books'; // replace with the slug of the category you want to check $order = wc_get_order( $order_id ); $items = $order->get_items(); foreach ( $items as $item ) { $product = $item->get_product(); $categories = $product->get_category_ids(); if ( in_array( $category_slug, $categories ) ) { // the category is in the order // you can do something here, like set a flag or output a message break; // exit the loop since you found the category } } |
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.
Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co