按 id 或 sku 在后端订单列表中扩展对产品项目的搜索 [英] Extending search in backend orders list for product items by id or by sku

查看:49
本文介绍了按 id 或 sku 在后端订单列表中扩展对产品项目的搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 在 Woocommerce 订单管理页面中按订单项 SKU 或 ID 搜索 以启用按天空和 ID 搜索 woocommerce 订单,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'));
});

当我将其添加到我的functions.php时,出现以下错误:

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

我假设由于这是大约 1 年的历史并且 woocommerce 在那段时间经历了一些重大变化,因此该代码需要以某种方式更新,但是我对 woocommerce 的经验不足,无法识别出问题所在.

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.

谢谢

推荐答案

更新:与 WooCommerce 3+ 的兼容性

Updated: compatibility with WooCommerce 3+

虽然 woocommerce_shop_order_search_fields 过滤器钩子扩展了后端订单列表中的搜索,但您需要添加一些 post_meta 字段用于基于 id 或 sku 的订单项搜索.

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.

作者在产品skuadd_post_meta()犯了一个错误.您需要用 $search_sku 替换未定义的 $sku 变量,我认为.

更新: 在对这段代码片段中使用的 wordpress 函数进行一些思考和搜索之后,我想我已经找到了解决方案.

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

这是与到add_post_meta()函数问题相关的更新代码:

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'));
} );

缺少一个可选参数,当设置为 true 时,该参数指定 value 不是数组.

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

现在应该可以了.

参考:

https:WP codex add_post_meta()

这篇关于按 id 或 sku 在后端订单列表中扩展对产品项目的搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆