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        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>';     
   }
   
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

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. Follow @rmelogli

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? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. 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 the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *