doulu6234 2016-06-11 02: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 09: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:

    展开全部

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部