csa*_*as1 3 php wordpress custom-taxonomy woocommerce hook-woocommerce
我只想更改特定类别的产品档案上的添加到购物车文本。例如,在预订类别上,我想要而不是add to cart文本,是Preorder. 我不知道如何在下面的函数中识别 Preorder 类别。
add_filter( 'add_to_cart_text', 'woo_archive_custom_cart_button_text' ); // < 2.1
function woo_archive_custom_cart_button_text() {
return __( 'Preorder', 'woocommerce' );
}
Run Code Online (Sandbox Code Playgroud)
更新:
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;
}
Run Code Online (Sandbox Code Playgroud)
代码位于活动子主题(或活动主题)的 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;
}
Run Code Online (Sandbox Code Playgroud)
对于单个产品页面,您将另外使用:
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;
}
Run Code Online (Sandbox Code Playgroud)
代码位于活动子主题(或活动主题)的 function.php 文件中。
测试和工作。
注意:如果你设置了一些条件,所有过滤器挂钩函数都需要返回主参数,所以在这种情况下,参数
$text......
相关答案:从 WooCommerce 中的自定义分类法定位产品术语
相关文档:Woocommerce 条件标签