更新: add_to_cart_text 挂钩已过时且已弃用。它在 Woocommerce 3+ 中被 woocommerce_product_add_to_cart_text 过滤器钩子替换。
它可以是 2 个不同的东西 (因为你的问题不是很清楚)...
1) 要定位特定产品类别存档页面上的产品,您应该这样使用条件函数is_product_category():
add_filter( 'woocommerce_product_add_to_cart_text', 'product_cat_add_to_cart_button_text', 20, 1 );
function product_cat_add_to_cart_button_text( $text ) {
// Only for a specific product category archive pages
if( is_product_category( array('preorder') ) )
$text = __( 'Preorder', 'woocommerce' );
return $text;
}
代码进入活动子主题(或活动主题)的 function.php 文件中。
2) 要在 Woocommerce 档案页面上定位特定产品类别,您将使用 has term() 这种方式:
add_filter( 'woocommerce_product_add_to_cart_text', 'product_cat_add_to_cart_button_text', 20, 1 );
function product_cat_add_to_cart_button_text( $text ) {
// Only for a specific product category
if( has_term( array('preorder'), 'product_cat' ) )
$text = __( 'Preorder', 'woocommerce' );
return $text;
}
对于单个产品页面,您将另外使用:
add_filter( 'woocommerce_product_single_add_to_cart_text', 'product_cat_single_add_to_cart_button_text', 20, 1 );
function product_cat_single_add_to_cart_button_text( $text ) {
// Only for a specific product category
if( has_term( array('preorder'), 'product_cat' ) )
$text = __( 'Preorder', 'woocommerce' );
return $text;
}
代码进入活动子主题(或活动主题)的 function.php 文件中。
经过测试并且有效。
注意:如果你设置了一些条件,所有过滤器钩子函数都需要返回主参数,所以在这种情况下参数$text…
相关回答:Targeting product terms from a custom taxonomy in WooCommerce
相关文档:Woocommerce Conditional Tags