我想在变体名称后添加内容。
我找到这个钩子并看到它以前可以工作,但在我的测试中没有:(
我想问,是否有另一种钩子/工作方法可以用来添加内容?
add_filter('woocommerce_attribute_label', 'custom_attribute_label', 10, 3);
function custom_attribute_label($label, $name, $product){
$taxonomy = 'pa_' . $name;
if ($taxonomy == 'pa_size')
$label .= '<div class="custom-label">' . __('MY TEXT', 'woocommerce') . '</div>';
return $label;
}
如果您使用
$label
变量,它将返回不带 "pa_"
前缀的名称。有些变体有 "pa_"
前缀,有些则没有。
所以你不需要创建额外的变量(即$taxonomy)。只需使用
$label
变量,如下所示:
add_filter('woocommerce_attribute_label', 'custom_attribute_label', 10, 3);
function custom_attribute_label($label, $name, $product)
{
if ('Logo' == $label)
{
$label .= '<div class="custom-label">' . __('MY TEXT', 'woocommerce') . '</div>';
}
return $label;
}
您可以使用
switch
语句来检查多个条件,如下所示:
add_filter('woocommerce_attribute_label', 'custom_attribute_label', 10, 3);
function custom_attribute_label($label, $name, $product)
{
switch ($label)
{
case 'Logo':
$label .= '<div class="custom-label">' . __('MY TEXT AFTER LOGO', 'woocommerce') . '</div>';
break;
case 'Color':
$label .= '<div class="custom-label">' . __('MY TEXT AFTER COLOR', 'woocommerce') . '</div>';
break;
}
return $label;
}
美好的一天鲁维,
我使用上述解决方案在产品页面的属性标签下方提及文本,效果非常好。
但是,现在此文本也作为属性标签显示在购物车中。是否可以将其设为纯文本,而不是附加属性标签?那么它只会在产品页面上提及,而不会在购物车或订单后端等其他地方提及?