我只想更改特定类别的产品文件中添加到购物车的文本。例如,在 preorder category 上,我想要 Preorder
而不是 add to cart
文本。我不知道如何在下面的函数中识别预购类别。
add_filter( 'add_to_cart_text', 'woo_archive_custom_cart_button_text' ); // < 2.1
function woo_archive_custom_cart_button_text() {
return __( 'Preorder', 'woocommerce' );
}
最佳答案
Update:
add_to_cart_text
hook is obsolete & deprecated. It is replaced in Woocommerce 3+ bywoocommerce_product_add_to_cart_text
filter hook.
它可以是 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 文件中。
经过测试并且有效。
Note: All filter hooked functions needs to return the main argument if you set some conditions, so in this case the argument
$text
…
相关答案:Targeting product terms from a custom taxonomy in WooCommerce
关于php - 在 Woocommerce 3 中更改特定产品类别的添加到购物车文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48354487/