【发布时间】:2021-06-21 20:50:21
【问题描述】:
注意:此问题被标记为与其他两个问题相似。这 不正确,因为另外两个问题与显示自定义有关 场数据。我不想那样做。我只是想显示 所选变体的名称。
我想做的是在产品标题下、购物车、订单确认页面和订单电子邮件中显示所选变体的名称。
我想通过过滤器和函数来实现这一点,而不是创建自定义模板文件。几年前我在另一个网站上做过这个。但不记得我使用的解决方案。
我发现了许多与此相关的问题和答案,但没有成功让其中任何一个起作用。我怀疑这部分是由于 WooCommerce 的变化。但即使是一些更新的和更新的答案也不起作用。
例如,this 听起来很接近我想要的(用于在购物车中显示变体名称)。
提供的答案使用了这个代码:
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', '<' ) ? $product_item->get_variation_description() : $product_item->get_description();
$result = __( 'Description: ', 'woocommerce' ) . $descrition;
return $name . '<br>' . $result;
} else
return $name;
}
在子主题'functions.php'中使用它,我发现这根本没有效果。
我还尝试了here 提供的答案。在那种情况下,它将显示brand 属性和类别。我将brand 换成了size(我的属性/变体的名称),但它根本没有输出。
【问题讨论】:
标签: php wordpress woocommerce