1 php wordpress product button woocommerce
在 woocommerce 中,我可以使用以下代码将所有“添加到购物车”按钮文本更改为“+”:
add_filter('woocommerce_product_add_to_cart_text',
'wh_archive_custom_cart_button_text'); // 2.1 +
function wh_archive_custom_cart_button_text()
{
return __('+', 'woocommerce');
}
Run Code Online (Sandbox Code Playgroud)
但现在我想将特定产品的按钮文本更改为(小写) “添加到购物车”。
感谢一些帮助。
以下代码将根据定义的产品 ID 更改“添加到购物车”按钮文本:
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 ) {
// HERE define your specific product IDs in this array
$specific_ids = array(37, 40, 53);
if( in_array($product->get_id(), $specific_ids) ) {
$button_text = __("add to cart", "woocommerce");
} else {
$button_text = __('+', 'woocommerce');
}
return $button_text;
}
Run Code Online (Sandbox Code Playgroud)
代码位于活动子主题(或活动主题)的 function.php 文件中。经过测试并有效。