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

Adding a custom text next to a specific product attribute label in WooCommerce

尝试在 title/label 旁边添加文本(最好在 div 内)以获得变体(即 SIZE)而不编辑 woocommerce 页面模板 .

伪代码:

function my_text_after_variation (){
  if attribute pa_size exists {
    echo label for pa_size;
    echo <div> MY TEXT </div>;
}else{
   return;
}};

非常欢迎任何帮助。

如果您无法编辑 woocommerce 页面模板,请尝试使用 jQuery。 例如:

$('.pa_size label').after('<span class="some-text">some text</text>');

如果您需要使用可变文本,您可以使用“wp_localize_script”创建带有此变量的对象并从该对象中获取它:

function.php

// Localize the script with your data
$data_array = array(
  'some_text' => __( 'Some text to insert', 'my-domain' ),
);
wp_localize_script( 'some_handle', 'object_name', $data_array );

jQuery:

$('.pa_size label').after(object_name.some_text);

要在特定属性标签名称之后显示自定义文本,您将使用这个挂钩在 woocommerce_attribute_label 专用过滤器挂钩中的自定义函数,这样:

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

代码进入您的活动子主题(或活动主题)的 function.php 文件。

已测试并有效。