WooCommerce 在变体标签后显示内容

Posted

技术标签:

【中文标题】WooCommerce 在变体标签后显示内容【英文标题】:WooCommerce show content after variation labels 【发布时间】:2022-01-18 22:30:38 【问题描述】:

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

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

我想问一下,有没有其他的钩子/工作方法可以用来添加内容?

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;

代码参考:Adding a custom text next to a specific product attribute label in WooCommerce

【问题讨论】:

【参考方案1】:

如果您使用$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;

【讨论】:

以上是关于WooCommerce 在变体标签后显示内容的主要内容,如果未能解决你的问题,请参考以下文章