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
 * @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;
   // 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

  • WooCommerce: Get Order Info (total, items, etc) From $order Object
    As a WooCommerce development freelancer, every day I repeat many coding operations that make me waste time. One of them is: “How to get ____ if I have the $order variable/object?“. For example, “How can I get the order total“? Or “How can I get the order items“? Or maybe the order dates, customer ID, […]
  • WooCommerce: How to Add a Custom Checkout Field
    Let’s imagine you want to add a custom checkout field (and not an additional billing or shipping field) on the WooCommerce Checkout page. For example, it might be a customer licence number – this has got nothing to do with billing and nothing to do with shipping. Ideally, this custom field could show above the […]
  • WooCommerce: Allow Users to Edit Processing Orders
    How can WooCommerce customers edit an order they just placed and paid for? I swear I looked on search engine results and other places before coming to the conclusion I needed to code this myself. For example, a user might want to change the delivery date (if you provide this on the checkout page). Or […]
  • WooCommerce: Create a Custom Order Status
    All WooCommerce orders go to either “processing”, “completed”, “on-hold” and other default order statuses based on the payment method and product type. Sometimes these statuses are not enough. For example, you might need to mark certain orders in a different way for tracking, filtering, exporting purposes. Or you might want to disable default emails by […]
  • WooCommerce: Add Column to Orders Table @ WP Dashboard
    The WooCommerce Orders Table, which can be found under WP Dashboard > WooCommerce > Orders, provides us with 7 default columns: Order – Date – Status – Billing – Ship to – Total – Actions. This is used by shop managers to have an overview of all orders, before eventually clicking on a specific one. […]

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…



    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 *