【发布时间】:2018-02-01 14:52:10
【问题描述】:
我想在购物车(在产品列下)和结帐页面上显示 SKU。
我搜索了 SO,但所有答案都是针对旧版本的 WooCommerce,而不是针对 3.x。
如何在 Woocommerce 3 的购物车和结帐页面上显示 SKU?
【问题讨论】:
标签: php wordpress woocommerce checkout cart
我想在购物车(在产品列下)和结帐页面上显示 SKU。
我搜索了 SO,但所有答案都是针对旧版本的 WooCommerce,而不是针对 3.x。
如何在 Woocommerce 3 的购物车和结帐页面上显示 SKU?
【问题讨论】:
标签: php wordpress woocommerce checkout cart
2021 年更新
您可以使用挂在 woocommerce_cart_item_name 动作钩子中的自定义函数来实现,这样:
// Display the sku below cart item name
add_filter( 'woocommerce_cart_item_name', 'display_sku_after_item_name', 5, 3 );
function display_sku_after_item_name( $item_name, $cart_item, $cart_item_key ) {
$product = $cart_item['data']; // The WC_Product Object
if( is_cart() && $product->get_sku() ) {
$item_name .= '<br><span class="item-sku">'. $product->get_sku() . '</span>';
}
return $item_name;
}
// Display the sku below under cart item name in checkout
add_filter( 'woocommerce_checkout_cart_item_quantity', 'display_sku_after_item_qty', 5, 3 );
function display_sku_after_item_qty( $item_quantity, $cart_item, $cart_item_key ) {
$product = $cart_item['data']; // The WC_Product Object
if( $product->get_sku() ) {
$item_quantity .= '<br><span class="item-sku">'. $product->get_sku() . '</span>';
}
return $item_quantity;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。
此代码经过测试,可在 WooCommerce 3+ 上运行。你会得到:
和
相关类似:How to Show SKU with product title in Order received page and Email order
【讨论】:
你可以这样做:
/**
* @snippet Show SKU @ WooCommerce Cart
* @author Khalid Almallahi
*/
add_action( 'woocommerce_after_cart_item_name', 'abukotsh_sku_below_cart_item_name', 11, 2 );
function abukotsh_sku_below_cart_item_name( $cart_item, $cart_item_key ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$sku = $_product->get_sku();
if ( ! $sku ) return;
echo '<p><small>SKU: ' . $sku . '</small></p>';
}
【讨论】:
您需要做一些模板覆盖。
购物车
将plugins/woocommerce/templates/cart/cart.php 复制到您的主题文件my_theme/woocommerce/cart/cart.php(如果它不存在)。然后在大约 #85 行添加以下内容
// Meta data
// (this is already in cart.php; look for it for where to place the next snippet)
echo WC()->cart->get_item_data( $cart_item );
// Add SKU below product name
if ( !empty($_product->get_sku()) ) { ?>
<div class="sku">
<small><?php echo "SKU: ". $_product->get_sku(); ?></small>
</div>
<?php }
结帐
将plugins/woocommerce/templates/checkout/review-order.php 复制到您的主题文件my_theme/woocommerce/checkout/review-order.php(如果它不存在)。然后在大约 #43 行添加以下内容
<?php
// (this is already in review-order.php; look for it for where to place the next snippet)
echo WC()->cart->get_item_data( $cart_item ); ?>
<?php
// Add SKU below product name
if ( !empty($_product->get_sku()) ) { ?>
<div class="sku">
<small><?php echo "SKU: ". $_product->get_sku(); ?></small>
</div>
<?php } ?>
【讨论】: