March 21, 2015 John Arcadian no responses

Woocommerce Search Orders By Item or Sku Plugin

A while back a client, by which I mean every client I’ve ever had with a woocommerce store on their site, asked me if they can search through their orders to find all orders that contain a certain item. As my introduction to Woocommerce was a little over a year ago, I knew the functionality wasn’t there but I wasn’t sure how to add it.

Being the good forward thinker that I am, I put it on my “Deal with this eventually list” and proceeded to move on to issues that had deadlines and that clients were actually paying me to fix for them. In a few moment of downtime the other week, I decided to revisit the issue. Much to my delight, I found some definitive answers on how to make this happen.

This recent post on Stack Exchange contained code and a solution cobbled together by 3 or 4 different users.

http://stackoverflow.com/questions/28063920/search-by-order-item-sku-or-id-in-woocommerce-orders-admin-page

I grabbed it, tested it on a few of my test sites, and gleefully found that it added the exact ability so many of my clients had been looking for. Searching for these answers before last month turned up many scarce results, so I decided to wrap the code in an easy to install plugin and throw it out there for anyone who might need it. Please be aware of a few things.

  • wooninjaI did not write this code, merely compiled the solution from the stack exchange thread into a plugin so all you have to do is install and not worry about modifying your functions file in your theme.
  • Code for this plugin was gathered from this post on stack overlow and is made available in plugin form for ease
  • http://stackoverflow.com/questions/28063920/search-by-order-item-sku-or-id-in-woocommerce-orders-admin-page
  • Code comes from Stack Overflow Users blacksquare, jibby, helgatheviking, and the person who created the post Nikos
  • I offer no guarantees and take no responsibility for the use of this code.
  • I will not be supporting or updating this
Download Woocommerce Search Orders By Item or Sku Plugin

 


 

For those who don’t want the plugin, just the code – here it is:

// Code for this plugin was gathered from this post on stack overlow and is made available in plugin form for ease
// http://stackoverflow.com/questions/28063920/search-by-order-item-sku-or-id-in-woocommerce-orders-admin-page
// Code comes from Stack Overflow Users blacksquare, jibby, helgatheviking, and the person who created the post Nikos

add_filter( ‘woocommerce_shop_order_search_fields’, function ($search_fields ) {
$posts = get_posts(array(‘post_type’ => ‘shop_order’));

foreach ($posts as $post) {
$order_id = $post->ID;
$order = new WC_Order($order_id);
$items = $order->get_items();

foreach($items as $item) {
$product_id = $item[‘product_id’];
$search_sku = get_post_meta($product_id, “_sku”, true);
add_post_meta($order_id, “_product_sku”, $search_sku);
}
}

return array_merge($search_fields, array(‘_product_sku’));
});

 

Share it!
Aenean mattis venenatis
John Arcadian is a writer, gamer, art director, web designer, crafter, and kilt-wearer. You can find more of his writings on gaming over at http://gnomestew.com. For web-design projects, check out http://beezenwebdesign.com.

Leave a Reply

Your email address will not be published.

John Arcadian