WooCommerce 在变体标签后显示内容

问题描述 投票:0回答:2

我想在变体名称后添加内容。

我找到这个钩子并看到它以前可以工作,但在我的测试中没有:(

我想问,是否有另一种钩子/工作方法可以用来添加内容?

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;
}

代码参考:
在 WooCommerce 中的特定产品属性标签旁边添加自定义文本

php wordpress woocommerce hook-woocommerce woocommerce-theming
2个回答
5
投票

如果您使用

$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;
}


0
投票

美好的一天鲁维,

我使用上述解决方案在产品页面的属性标签下方提及文本,效果非常好。

但是,现在此文本也作为属性标签显示在购物车中。是否可以将其设为纯文本,而不是附加属性标签?那么它只会在产品页面上提及,而不会在购物车或订单后端等其他地方提及?

© www.soinside.com 2019 - 2024. All rights reserved.