Sunday, 8 May 2016

Search order by SKU in WooCommerce admin

The search functionality in woocommerce doesn't search by sku by default. Add bellow code snippet in functions.php you current theme's.



/**
 * Search product by SKU in Admin Woocommerce Orders
 */
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'));
});

No comments: