Woocommerce 更改特定类别的“选择选项”按钮文本

huangapple go评论77阅读模式
英文:

Woocommerce Change 'Select Options' button text for specific category

问题

我有以下更改可变产品的按钮文本的代码,从“选择选项”更改为“选择日期”。

add_filter('woocommerce_product_add_to_cart_text', 'change_select_options_button_text', 9999, 2);

function change_select_options_button_text($label, $product) {
   if ($product->is_type('variable')) {
      return '选择日期';
   }
   return $label;
}

我想将此代码仅应用于特定类别,而不是应用于每个可变产品。这是否可能?

我尝试添加以下内容:if ( is_product_category('cat1')

但是没有成功。

add_filter('woocommerce_product_add_to_cart_text', 'change_select_options_button_text', 9999, 2);

function change_select_options_button_text($label, $product) {
   if (is_product_category('cat1') && $product->is_type('variable')) {
      return '选择日期';
   }
   return $label;
}

任何帮助将不胜感激。
谢谢!

英文:

I have the following code that changes the button text of a variable product from 'select options' to select dates.

add_filter( 'woocommerce_product_add_to_cart_text', 'change_select_options_button_text', 9999, 2 );
 
function change_select_options_button_text( $label, $product ) {
   if ( $product->is_type( 'variable' ) ) {
      return 'Select Dates';
   }
   return $label;
}

I'm wanting to apply this only to a certain category instead of it applying to every variable product. Would this be possible?

I've tried adding : if ( is_product_category('cat1')

like so but have had no luck.

add_filter( 'woocommerce_product_add_to_cart_text', 'change_select_options_button_text', 9999, 2 );
 
function change_select_options_button_text( $label, $product ) {
   if ( is_product_category('cat1') && $product->is_type( 'variable' ) ) {
      return 'Select Dates';
   }
   return $label;
}

Any help would be greatly appreciated.
Thank You!

答案1

得分: 1

add_filter('woocommerce_product_add_to_cart_text', 'change_select_options_button_text', 9999, 2);

function change_select_options_button_text($label, $product) {
    if ($product->is_type('variable') && has_term(array('kids clothing'), 'product_cat', $product->get_id())) {
        // 如果产品是可变的,并且属于“kids clothing”类别,则执行某些操作
        return '选择日期';
    }
    return $label;
}
英文:
add_filter('woocommerce_product_add_to_cart_text', 'change_select_options_button_text', 9999, 2);

function change_select_options_button_text($label, $product) {
	if ($product->is_type('variable') && has_term(array('kids clothing'), 'product_cat', $product->get_id())) {
        // Do something if the product is variable and in category "kids clothing"
		return 'Select Dates';
	}
	return $label;
}

huangapple
  • 本文由 发表于 2023年1月9日 11:37:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75052959.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定