如果术语类别 + 是变量 WooCommerce,则尝试更改购买按钮文本
Trying to change purchase-button text if has term category + is variable WooCommerce
如果我删除 && $product->is_type( 'variable' )
一切正常
但是,当我添加该行时,第一个 if 似乎不起作用。我做错了什么?
预期:如果产品属于类别 "farsk-ved" 并且是可变产品,则文本应为 "Välj längd"。所以所有的单品,都应该去"else"版块("Köp").
发生了什么:除 "elseif" 部分外,所有产品都在 "köp"。
add_filter( 'woocommerce_product_add_to_cart_text', 'custom_loop_add_to_cart_button', 20, 2 );
function custom_loop_add_to_cart_button( $button_text ) {
// BELOW set the product categoty slug and the product ID
$product = new WC_Product( get_the_ID() );
if ( has_term( 'farsk-ved', 'product_cat') && $product->is_type( 'variable' )) {
$button_text = __("Välj längd", "woocommerce");
}
elseif ( has_term( 'torr-ved', 'product_cat') ) {
$button_text = __("Välj mängd", "woocommerce");
}
else {
$button_text = __("Köp", "woocommerce");
}
return $button_text;
}
如果你使用 $product = new WC_Product( get_the_ID() );
和 $product->is_type( 'variable' )
它将 return bool(false)
如果您使用 $product = new WC_Product( get_the_ID() );
和 $product->get_type()
,它将 return 简单 所有产品,包括变量类型。
所以你只需要这样使用,
$product = new WC_Product_Variable($product_id);
if ( has_term( 'farsk-ved', 'product_cat') && $product->get_type()=='variable') {
我认为这会有所帮助。
所以解决方案是替换 $product = new WC_Product( get_the_ID() );
global $product;
真正的方法: WC_Product
对象作为第二个参数直接包含在 woocommerce_product_add_to_cart_text
过滤器钩子中 ( 和 你的函数代码中缺少):
add_filter( 'woocommerce_product_add_to_cart_text', 'custom_loop_add_to_cart_button', 20, 2 );
function custom_loop_add_to_cart_button( $button_text, $product ) { // <== the missing argument
// BELOW set the product category Id, slug or name
if ( has_term( 'farsk-ved', 'product_cat') && $product->is_type( 'variable' ) ) {
$button_text = __("Välj längd", "woocommerce");
}
elseif ( has_term( 'torr-ved', 'product_cat') ) {
$button_text = __("Välj mängd", "woocommerce");
}
else {
$button_text = __("Köp", "woocommerce");
}
return $button_text;
}
已测试并有效。
如果我删除 && $product->is_type( 'variable' )
但是,当我添加该行时,第一个 if 似乎不起作用。我做错了什么?
预期:如果产品属于类别 "farsk-ved" 并且是可变产品,则文本应为 "Välj längd"。所以所有的单品,都应该去"else"版块("Köp").
发生了什么:除 "elseif" 部分外,所有产品都在 "köp"。
add_filter( 'woocommerce_product_add_to_cart_text', 'custom_loop_add_to_cart_button', 20, 2 );
function custom_loop_add_to_cart_button( $button_text ) {
// BELOW set the product categoty slug and the product ID
$product = new WC_Product( get_the_ID() );
if ( has_term( 'farsk-ved', 'product_cat') && $product->is_type( 'variable' )) {
$button_text = __("Välj längd", "woocommerce");
}
elseif ( has_term( 'torr-ved', 'product_cat') ) {
$button_text = __("Välj mängd", "woocommerce");
}
else {
$button_text = __("Köp", "woocommerce");
}
return $button_text;
}
如果你使用 $product = new WC_Product( get_the_ID() );
和 $product->is_type( 'variable' )
它将 return bool(false)
如果您使用 $product = new WC_Product( get_the_ID() );
和 $product->get_type()
,它将 return 简单 所有产品,包括变量类型。
所以你只需要这样使用,
$product = new WC_Product_Variable($product_id);
if ( has_term( 'farsk-ved', 'product_cat') && $product->get_type()=='variable') {
我认为这会有所帮助。
所以解决方案是替换 $product = new WC_Product( get_the_ID() );
global $product;
真正的方法: WC_Product
对象作为第二个参数直接包含在 woocommerce_product_add_to_cart_text
过滤器钩子中 ( 和 你的函数代码中缺少):
add_filter( 'woocommerce_product_add_to_cart_text', 'custom_loop_add_to_cart_button', 20, 2 );
function custom_loop_add_to_cart_button( $button_text, $product ) { // <== the missing argument
// BELOW set the product category Id, slug or name
if ( has_term( 'farsk-ved', 'product_cat') && $product->is_type( 'variable' ) ) {
$button_text = __("Välj längd", "woocommerce");
}
elseif ( has_term( 'torr-ved', 'product_cat') ) {
$button_text = __("Välj mängd", "woocommerce");
}
else {
$button_text = __("Köp", "woocommerce");
}
return $button_text;
}
已测试并有效。