Woocommerce Search Orders By Item or Sku Plugin

//Woocommerce Search Orders By Item or Sku Plugin

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.

  • I 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.
  • wooninjaCode 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

So, with that out of the way, grab the plugin, install and enjoy.

 


 

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’));
});

 

John Arcadian is the owner and Lead Developer for Bee Zen Web Design. He has a passion for creating incredible sites and loves working with clients to solve unique challenges. When he isn’t creating websites, he is writing books, doing art direction, or walking around in his kilt.