We already saw how to check if a product category is in the cart, if a product ID is in the cart, and if a product ID is in the order… now it’s time to complete the series with the latest addition!
For this client, the scope was to do something on the “Thank You” page if a certain product category was purchased. For example, echo a “Thank you for becoming a member!” image in case the category “membership” was in the order.
Here’s the snippet, together with PHP comments so that you can understand how this is done. Enjoy!
PHP Snippet: Check if Product Category is in the Order – WooCommerce
/**
* @snippet Check if Product Category is in the Order
* @how-to businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @compatible Woo 4.0
* @community https://businessbloomer.com/club/
*/
add_action( 'woocommerce_thankyou', 'bbloomer_custom_woocommerce_auto_complete_order', 5 );
function bbloomer_custom_woocommerce_auto_complete_order( $order_id ) {
// 1. Get order object
$order = wc_get_order( $order_id );
// 2. Initialize $cat_in_order variable
$cat_in_order = false;
// 3. Get order items and loop through them...
// ... if product in category, edit $cat_in_order
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item->get_product_id();
if ( has_term( 'memberships', 'product_cat', $product_id ) ) {
$cat_in_order = true;
break;
}
}
// 4. Echo image only if $cat_in_order == true
if ( $cat_in_order ) {
echo '<p><img src="https://....."></p>';
}
}
This is working, but I need the image to appear inside an existing div I have in my thankyou.php template. How would this be possible?
If I copy your code into thankyou.php it doesn’t work…
Regards,
Fabian
Hey Fabian, if it’s a custom DIV you could echo the whole DIV… otherwise it becomes a little complex 🙂
I cannot get this to work. Woocommerce 3.5.3 I see some things have changed when I compare your coding to new woocommerce files and when I var_dump $order_id i get null. Is there any possible way to get this updated. Or might there be something else going on with my setup?
Hello D, thanks so much for your comment! I just retested this on the latest version of WooCommerce and it still works. Unfortunately this looks like custom troubleshooting work and I cannot help here via the blog comments. Thanks a lot for your understanding! ~R
Hi, thank you for this tutorial.
I am trying to check whether an order does not include a simple product.
I want to remove an order status when there are only “downloadable products” in the order.
I have a custom order status called “dispatch” which I manually trigger to go to the Logistics department once Accounts can confirm that payment was made successfully. But I do not want this status option in the backend when the order contain only downloadable products.
So this is my function, but I need to make it conditional for only when there are only downloadable products in the order:
Thank you
Hey Schak, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R