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:

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

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?