doulu6234 2016-06-11 10:51
浏览 80
已采纳

通过id或sku扩展产品项目的后端订单列表中的搜索

Im trying to use the following code from Search by order item SKU or ID in Woocommerce Orders Admin page to enable searching woocommerce orders by sky and id, sku being the important part for me.

   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", $sku);
            add_post_meta($order_id, "_product_id", $product_id);
        }
    }

    return array_merge($search_fields, array('_product_sku', '_product_id'));
});

When I add this to my functions.php I get the following errors:

Array() expects parameter 1 to be a valid callback, function 'woocommerce_shop_order_search_order_total' not found or invalid function name in /var/sites/s/silverfx.co.uk/public_html/wp-includes/plugin.php on line 235

Warning: array_merge(): Argument #1 is not an array in /var/sites/s/silverfx.co.uk/public_html/wp-content/themes/SilverFx-Theme/functions.php on line 156

Warning: array_map(): Argument #2 should be an array in /var/sites/s/silverfx.co.uk/public_html/wp-content/plugins/woocommerce/includes/admin/class-wc-admin-post-types.php on line 1533

Warning: array_map(): Argument #2 should be an array in /var/sites/s/silverfx.co.uk/public_html/wp-content/plugins/woocommerce/includes/admin/class-wc-admin-post-types.php on line 1557

Warning: implode(): Invalid arguments passed in /var/sites/s/silverfx.co.uk/public_html/wp-content/plugins/woocommerce/includes/admin/class-wc-admin-post-types.php on line 1557

Im making the assumption that since this is about 1 year old and woocommerce have gone through some big changes in that time, this code is needs updating in someway however i am not experienced enough with woocommerce to recognise what is wrong.

If anyonce out there is able to just confirm I am on the correct path or offer guidence / suggestions as to what I might need to do that would be great.

Thanks

  • 写回答

1条回答 默认 最新

  • doukanwa6872 2016-06-11 17:37
    关注

    Updated: compatibility with WooCommerce 3+

    While woocommerce_shop_order_search_fields filter hook extend search in backend orders list, you need to add some post_meta fields for order item(s) search based on id or sku.

    The author have made a mistake in product sku add_post_meta(). You need to replace undefined $sku variable by $search_sku instead, I think.

    Update: after some thinking and some search around wordpress functions used in this code snippet, I think i have got the solution.

    Here is the updated code related to add_post_meta() function issue:

    add_filter( 'woocommerce_shop_order_search_fields', function ($search_fields ) {
        $orders = get_posts( array( 'post_type' => 'shop_order' ) );
    
        foreach ($orders as $order_post) {
            $order_id = $order_post->ID;
            $order = new WC_Order($order_id);
            $items = $order->get_items();
    
            foreach( $order->get_items() as $item_id => $item_values ) {
                if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
                    $product_id = $item_values['product_id'];
                } else {
                    $product_id = $item_values->get_product_id();
                }
                $search_sku = get_post_meta($product_id, "_sku", true);
                add_post_meta($order_id, "_product_id", $product_id, true); //  <= ## Here ##
                add_post_meta($order_id, "_product_sku", $search_sku, true); // <= ## Here ##
            }
        }
        return array_merge($search_fields, array('_product_id', '_product_sku'));
    } );
    

    There is a missing optional argument, that specify that the value is not an array, when set to true.

    This should work now.

    Reference:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?