WooCommerce: Check if Product Category is in the Order

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!

Check if Product Category is in the WooCommerce Order

PHP Snippet: Check if Product Category is in the Order – WooCommerce

/**
 * @snippet       Check if Product Category is in the Order
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    Woo 4.0
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
 
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>';     
   }
   
}

Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (delete "?>" if you have it there). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.3.

If you think this code saved you time & money, feel free to join 14,000+ WooCommerce Weekly subscribers for blog post updates or 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

6 thoughts on “WooCommerce: Check if Product Category is in the Order

  1. 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

    1. Hey Fabian, if it’s a custom DIV you could echo the whole DIV… otherwise it becomes a little complex 🙂

  2. 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?

    1. 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

  3. 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:

     Set Here the WooCommerce icon for your action button
    add_action( 'admin_head', 'hide_dispatch_for_downloads' );
        function hide_dispatch_for_downloads() {
           echo '<style>.widefat .column-order_actions a.dispatch { display: none; }</style>';
    }

    Thank you

    1. 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

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)

Your email address will not be published.