php 购物车列表显示,php - Woocommerce:在购物车页面上显示产品变化描述

我正在尝试在购物车中显示产品变型说明。我已经尝试将此代码插入cart.php模板中:if ( $_product->is_type( 'variation' ) ) {echo $_product->get_variation_description();}

通过遵循本文档https://docs.woocommerce.com/document/template-structure/

但是它仍然没有出现。

不知道我在做什么错。

有人可以帮忙吗?

谢谢

最佳答案

UPDATE COMPATIBILITY for WooCommerce version 3+

从WooCommerce 3开始,现在不推荐使用get_variation_description()并将其替换为WC_Product方法get_description()。

To get your product item variation description in cart (filtering variation product type condition), you have 2 possibilities (may be even more…):

使用woocommerce_cart_item_name钩子(Hook)显示变体描述,而无需编辑任何模板。

使用cart.php模板显示版本描述。

In both cases you don't need to use in your code a foreach loop, as answered before, because it already exist. So the code will be more compact.

案例1-使用woocommerce_cart_item_name钩子(Hook):

add_filter( 'woocommerce_cart_item_name', 'cart_variation_description', 20, 3);

function cart_variation_description( $name, $cart_item, $cart_item_key ) {

// Get the corresponding WC_Product

$product_item = $cart_item['data'];

if(!empty($product_item) && $product_item->is_type( 'variation' ) ) {

// WC 3+ compatibility

$descrition = version_compare( WC_VERSION, '3.0', 'get_variation_description() : $product_item->get_description();

$result = __( 'Description: ', 'woocommerce' ) . $descrition;

return $name . '
' . $result;

} else

return $name;

}

在这种情况下,说明仅显示在标题和变体属性值之间。

这段代码会出现在您 Activity 的子主题(或主题)的function.php文件中,也可能会出现在任何插件文件中。

案例2-使用cart/cart.php模板(根据您的评论更新)。

您可以选择要显示此描述的位置(2个选择):

在标题之后

在标题和变体属性值之后。

因此,根据您的选择,您将在第86或90行附近的cart.php模板中插入以下代码:

// Get the WC_Product

$product_item = $cart_item['data'];

if( ! empty( $product_item ) && $product_item->is_type( 'variation' ) ) {

// WC 3+ compatibility

$description = version_compare( WC_VERSION, '3.0', 'get_variation_description() : $product_item->get_description();

if( ! empty( $description ) ) {

// '
'. could be added optionally if needed

echo __( 'Description: ', 'woocommerce' ) . $description;;

}

}

所有代码都经过测试并且功能齐全

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值