The WooCommerce Order Admin allows you to search for orders by the following fields:

  • order key
  • billing first name
  • billing last name
  • billing company
  • billing address 1
  • billing address 2
  • billing city
  • billing postcode
  • billing country
  • billing state
  • billing email
  • billing phone
  • order items

But what if you want to search by another order field? Perhaps the order total? Well it’s as easy as adding something like the following to your themes functions.php:

function woocommerce_shop_order_search_order_total( $search_fields ) {

  $search_fields[] = '_order_total';

  return $search_fields;
}
add_filter( 'woocommerce_shop_order_search_fields', 'woocommerce_shop_order_search_order_total' );

With that simple snippet active you can now search by order total. Note that with the numerical fields like total, you’d need to search with no thousands separator, and a period for the decimal place holder, ie: 1000.50 regardless of how you have configured WooCommerce to display totals on the frontend. That’s due to the way the numbers are stored within WooCommerce. Using this technique you can even search by custom post meta that you may be adding to the order records.

Published by Nik McLaughlin

You can find Nik around the WP space, on LinkedIn, or on his personal blog.

22 Comments

  1. Hi

    I am working on woocommerce site for online grocery shopping .
    I have stucked at customized requirement in which a customer after completion of his/her shopping the order get saved at wp_posts ..rgt..
    Now the same customer after one month search for the saved order (previous order).
    how should i retrieve this order saved data

    please help me out…

  2. Question — How do you search by order meta / options? For instance, when someone chooses a delivery date, or a variation of a product it’s stored as meta on the order. I can’t figure out what field and syntax to append to your code to make it work.

    Tried _order_custom_fields — but perhaps I’m using the wrong syntax to add it to your code.

    • If you know the meta key of the order meta you want to include, you can add it like: $Search_fields[] = '_some_meta_key_name'; — including meta for products included in the order would be a bit more complex.

      • can elaborate on “a bit more complex” at all? I think this is exactly what I need…to include wp_woocommerce_order_itemmeta data in the search to retrieve orders with a particular serial number, which I am storing in wp_woocommerce_order_itemmeta. Thank you!

        • Hey, knowlton, well the complexity comes in because the technique outlined above takes advantage of the fact that order meta (stored in wp_postmeta) can be easily included in the search query. Extending the search query to include item meta would be a whole other beast; it should be possible, at least in the sense that *anything* is possible, but it won’t be easy and probably involve mucking around with filters deep in the core WP_Query class. Alternatively though, you could perhaps set something up to stuff all the item meta values you’re using into a custom order meta field simply for the purposes of being available to search over. Then you’d use the technique described in the article to add that special meta field to the search query. Not the most ideal or elegant solution perhaps but it probably would be a lot easier and get the job done

  3. Does this discussion of metadata also refer to “custom fields” defined, for example, in woocommerce checkout manager?
    I’m looking to do two things:
    1. Add the custom metadata defined at checkout to the order and pass it to freshbooks
    2. pull reports based on that meta data
    Any help greatly appreciated
    Joel

  4. Any thoughts on how to do a search for both First and Last Name? First OR Last name will return something but both will just return nothing!

  5. If I created a custom field in woocommerce product, “Specification File” with a link, how would I call so that it displays on the UI vs the admin? I’m new to php and woocommerce and trying to add this to the additional information tab for the product. THx.

    • The additional info tab is a template file I believe, so you could override that and use the global $product object to get your custom field value with get_post_meta() — this should get you started in the right direction.

  6. Where do you find the meta key of the order meta data in the docs. I’ve finally managed to Google it all, but couldn’t find it in the Woocommerce docs. I can now search by payment type (_payment_method_title) ((though I really want to filter the orders page by payment type – if you’ve got a quick hint/snippet?)

    Gareth Jone

  7. Hello. Thanks for your article! It’s great for PHP beginners like myself.

    When I read your code, I can understand what it is saying. However I am trying to teach myself how to do this as well. And it’s harder to write code rather than understand it!

    In this line here:
    add_filter( ‘woocommerce_shop_order_search_fields’, ‘woocommerce_shop_order_search_order_total’ );

    The code is saying to add an additional field to that search bar, correct? How did you know in this case that woocommerce_shop_order_search_fields was the correct phrase to callout that search bar and that woocommerce_shop_order_search_order_total was the correct phrase to tell it to search the total price of orders?

    When I look up the hooks page here – http://docs.woocommerce.com/document/hooks/ I don’t see those hooks defined?

    Thanks for your help!!

  8. Is there a way to search in the order comments as well? This seems to be excluded and if I use your code and change it to $search_fields[] = ‘_order_comments’; (which should be correct, I have another filter successfully using order_comments), the order search still doesn’t bring up anything 🙁

    Any ideas? Thanks!

  9. Hey,

    Am using woo commerce gravity form addon, how do i search for the gravity addon form fields via the woo commerce search , i requested user to fill in name i.e. first and last as well as age now i wanna search for the age via the woo commerce search. any help ?

  10. Hi everybody,

    Great thread.

    I’m having some trouble with my WooCommerce and the WordPress forum seems to be next to useless.

    Basically, On my website, I sell sample packs of drinks.

    When running a report, I can easily see how many sample packs have been sold but the problem is that I need to know exactly how many, lets say, lemonades were in those sample packs.

    I can use the search bar in the “Orders” page to search for all orders containing a “sample pack” but it will not search the attributes/options within that order.

    Is there any way to acquire this information? Otherwise I will have to go through over a thousand printed invoices 🙁

    Any advice would be greatly appreciated.

    Thanks in advance.

    Scott

  11. Hi everybody,

    Why I can’t search by order item SKU?

    add_filter( 'woocommerce_shop_order_search_fields', 'woocommerce_shop_order_search_sku' );
     
    function woocommerce_shop_order_search_sku( $search_fields ) {
     
      $search_fields[] = '_sku';
     
      return $search_fields;
     
    }
    

    What else should I do?

    • Hey Nikos, you only have order meta accessible in that filter, not meta associated with products in the order. You’d probably have to do a custom SQL query to search by SKU.

Hmm, looks like this article is quite old! Its content may be outdated, so comments are now closed.