【问题标题】:WooCommerce show content after variation labelsWooCommerce 在变体标签后显示内容
【发布时间】: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

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce woocommerce-theming


    【解决方案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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-18
      • 1970-01-01
      • 1970-01-01
      • 2014-09-28
      • 2020-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多