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.
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…
Hi Swapna, the code in this article should help get your started 🙂
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
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
If you’re using the Freshbooks extension, you’d probably have to modify it to pass your custom meta data.
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!
This might be something that should be added to WooCommerce core (if it’s not already) — try logging an issue on https://github.com/woothemes/woocommerce/issues
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 withget_post_meta()
— this should get you started in the right direction.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
You can look at the checkout code to see what meta it sets (see https://github.com/woothemes/woocommerce/blob/master/includes/class-wc-checkout.php#L373) and/or look at the meta for in order in the DB to see the keys 🙂
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!!
Hi Matt, yep, the code is adding an accepted search field to that search bar. We knew that
woocommerce_shop_order_search_fields
was a filter that already exists mostly because we use WooCommerce all of the time: https://github.com/woothemes/woocommerce/blob/master/includes/admin/class-wc-admin-post-types.php#L1229 (not every filter is documented I’m afraid – some you learn from experience). This is a great resource for this: http://code.tutsplus.com/articles/the-beginners-guide-to-wordpress-actions-and-filters–wp-27373The
woocommerce_shop_order_search_order_total
is in that line because we’ve made that the name of our function that adds_order_total
to the array of search fields. We use the filter to add our own field via that function in theadd_filter( 'woocommerce_shop_order_search_fields', 'woocommerce_shop_order_search_order_total' );
line.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!
Hi Alex,
Unfortunately this works for meta keys only. Order notes are stored as default WordPress comments in the wp_comments table. You *may* be able to filter into ‘parse_query’ to achieve the desired result. To help you with this task, you can take a look at the method WooCommerce core uses to search by custom field: https://github.com/woothemes/woocommerce/blob/v2.2-beta-3/includes/admin/class-wc-admin-post-types.php#L1213-L1288
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 ?
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
Hi everybody,
Why I can’t search by order item SKU?
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.