如何在收到的订单页面和电子邮件订单中显示带有产品标题的 SKU [英] How to Show SKU with product title in Order received page and Email order
问题描述
在functions.php中
add_action( 'woocommerce_add_order_item_meta', 'custom_add_item_sku', 10, 3 );函数 custom_add_item_sku( $item_id, $values, $cart_item_key ) {$item_sku = get_post_meta( $values[ 'product_id' ], '_sku', true );wc_add_order_item_meta( $item_id, 'SKU', $item_sku , false );}
但它在产品标题下方显示 SKU 值,但我想要这样
<块引用><块引用>SKU:123sd - 产品名称
更新 2018-04-01 - 更改钩子和改进代码.
改用 woocommerce_order_item_name 过滤器钩子.我对您的代码进行了一些更改:
add_filter( 'woocommerce_order_item_name', 'sku_before_order_item_name', 30, 3 );函数 sku_before_order_item_name( $item_name, $item, $is_visible ) {$product = $item->get_product();$sku = $product->get_sku();//当 sku 不存在时,我们退出if( empty( $sku ) ) return $item_name;$sku_text = __( 'SKU', 'woocommerce') .':'.$sku;//当参数 $is_visible 为真时添加产品永久链接$product_permalink = $is_visible ?$product->get_permalink( $item ) : '';如果( $product_permalink )return sprintf('<a href="%s">%s - %s</a>', $product_permalink, $sku_text, $item->get_name() );别的返回 $sku_text .' - ' .$item->get_name();}
代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.
在 WooCommerce 3+ 中测试并有效.
<小时>在购物车商品名称中显示 SKU (更新:带有购物车页面的链接):
add_filter( 'woocommerce_cart_item_name', 'sku_before_cart_item_name', 10, 3 );函数 sku_before_cart_item_name( $product_name, $cart_item, $cart_item_key ) {$product = $cart_item['data'];$sku = $product->get_sku();//当 sku 不存在时,我们退出if( empty( $sku ) ) return $product_name;$sku_text = __( 'SKU', 'woocommerce') .':'.$sku;$product_permalink = $product->get_permalink( $cart_item );如果 ( is_cart() )return sprintf('<a href="%s">%s - %s</a>', esc_url( $product_permalink ), $sku_text, $product->get_name() );别的返回 $sku_text .' - ' .$产品名称;}
代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.
在 WooCommerce 3+ 中测试并有效.
In functions.php
add_action( 'woocommerce_add_order_item_meta', 'custom_add_item_sku', 10, 3 );
function custom_add_item_sku( $item_id, $values, $cart_item_key ) {
$item_sku = get_post_meta( $values[ 'product_id' ], '_sku', true );
wc_add_order_item_meta( $item_id, 'SKU', $item_sku , false );
}
But its showing SKU value below Product title but i want it like this
SKU:123sd - Product Name
Update 2018-04-01 - Changing of hook and Improved code.
Use woocommerce_order_item_name filter hook instead. I have make some changes in your code:
add_filter( 'woocommerce_order_item_name', 'sku_before_order_item_name', 30, 3 );
function sku_before_order_item_name( $item_name, $item, $is_visible ) {
$product = $item->get_product();
$sku = $product->get_sku();
// When sku doesn't exist we exit
if( empty( $sku ) ) return $item_name;
$sku_text = __( 'SKU', 'woocommerce' ) . ': ' . $sku;
// Add product permalink when argument $is_visible is true
$product_permalink = $is_visible ? $product->get_permalink( $item ) : '';
if( $product_permalink )
return sprintf( '<a href="%s">%s - %s</a>', $product_permalink, $sku_text, $item->get_name() );
else
return $sku_text . ' - ' . $item->get_name();
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested in WooCommerce 3+ and works.
Displaying the SKU in cart items name (Updated: with the link for cart page):
add_filter( 'woocommerce_cart_item_name', 'sku_before_cart_item_name', 10, 3 );
function sku_before_cart_item_name( $product_name, $cart_item, $cart_item_key ) {
$product = $cart_item['data'];
$sku = $product->get_sku();
// When sku doesn't exist we exit
if( empty( $sku ) ) return $product_name;
$sku_text = __( 'SKU', 'woocommerce' ) . ': ' . $sku;
$product_permalink = $product->get_permalink( $cart_item );
if ( is_cart() )
return sprintf( '<a href="%s">%s - %s</a>', esc_url( $product_permalink ), $sku_text, $product->get_name() );
else
return $sku_text . ' - ' . $product_name;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested in WooCommerce 3+ and works.
这篇关于如何在收到的订单页面和电子邮件订单中显示带有产品标题的 SKU的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!