Search by order item SKU or ID in WooCommerce Orders Admin page

前端 未结 4 1329
孤街浪徒
孤街浪徒 2020-12-31 15:50

What I am trying to do is to be able to search by order item SKU or ID in the WooCommerce Orders Admin page.

What I have found/done till now, but with no success is

4条回答
  •  不思量自难忘°
    2020-12-31 16:43

    @blacksquare, @jibby, @helgatheviking you are the men! This is the code that works, due to your help.

        //Search by product 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'));
    });
    

提交回复
热议问题