简体   繁体   English

在 Woocommerce 管理订单页面中用 SKU 替换产品名称

[英]Replace product name by SKU in Woocommerce admin orders pages

Again, and it will be my last question on this topic (I apologize if I asked too much questions), I would like to replace the Product name by the SKU only in Woocommerce --> Orders --> Order Details in the backend admin of WordPress.同样,这将是我关于这个主题的最后一个问题(如果我问的问题太多,我很抱歉),我想仅在 Woocommerce --> 订单 --> 后端管理中的订单详细信息中用 SKU 替换产品名称的 WordPress。

sku 代替产品名称

I didn't find any understandable explanation about how to do this on forums (I have really no skills with php code).我在论坛上没有找到关于如何做到这一点的任何可以理解的解释(我真的没有 php 代码的技能)。 I just found that the filter hooks that may be should be used is 'woocommerce_order_items' and 'woocommerce_order_itemmeta' but frankly speaking I don't understand how to use them.我刚刚发现可能应该使用的过滤器钩子是“woocommerce_order_items”和“woocommerce_order_itemmeta”,但坦率地说,我不明白如何使用它们。

Is it possible to know the correct code to replace the Product name by the SKU only in Woocommerce --> Orders --> Order Details in the backend admin of WordPress?是否可以仅在 Woocommerce --> 订单 --> WordPress 后端管理中的订单详细信息中知道用 SKU 替换产品名称的正确代码? If it is possible to have also some reference to understand about these hooks, I will really appreciate.如果有可能也有一些参考来了解这些钩子,我将不胜感激。 Thank you谢谢

You need to use the composite filter hook made from WC_Order_Item get_name() method like:您需要使用由WC_Order_Item get_name()方法制成的复合过滤器钩子,例如:

add_filter( 'woocommerce_order_item_get_name', 'filter_order_item_get_name', 10, 2 );
function filter_order_item_get_name( $item_name, $order_item ) {
    if ( is_admin() && $order_item->is_type('line_item') ) {
        $product = $order_item->get_product();

        if( $sku = $product->get_sku() ) {
            $item_name = $sku;
        }
    }
    return $item_name;
}

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and work.测试和工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM