[英]Change Add to cart button text for specific products in Woocommerce
In woocommerce I have been able to change all the "Add to cart" buttons text to "+", using the code below:在 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');
}
But now I would like to change the button text for specific products to (lowercase) "add to cart".但现在我想将特定产品的按钮文本更改为(小写) “添加到购物车”。
Some help is appreciated.一些帮助表示赞赏。
The following code will change add to cart button text depending on defined product IDs:以下代码将根据定义的产品 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;
}
Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and works.测试和工作。
If you want to change add to cart button text of a specific product without coding, here is the solution.如果您想在没有编码的情况下更改特定产品的添加到购物车按钮文本,这里是解决方案。
.postid-1324 .single_add_to_cart_button:after { content: ''; background: rgb(106,191,119); width: 110px; height: 45px; position: absolute; left: 0; right: 0; text-align: center; margin: 0 auto; } .postid-1324 .single_add_to_cart_button:before { content: 'add money to wallet'; position: absolute; z-index: 9999998; left: 0; right: 0; }
You can use an edited version of @LoicTheAztec 's code to apply it to single product page instances by editing the filter on the first line:您可以使用编辑后的 @LoicTheAztec 代码版本,通过编辑第一行的过滤器将其应用于单个产品页面实例:
add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_single_loop_add_to_cart_button', 20, 2 ); function custom_single_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; }
I ended up adding both the product archives page version (LoicTheAztec's code) and this single product page version to my functions.php file to cover all instances of a particular product.我最终将产品档案页面版本(LoicTheAztec 的代码)和这个单一产品页面版本都添加到了我的 functions.php 文件中,以涵盖特定产品的所有实例。 See this documentation from WooCommerce about the woocommerce_product_single_add_to_cart_text filter: https://woocommerce.com/document/change-add-to-cart-button-text/请参阅 WooCommerce 关于 woocommerce_product_single_add_to_cart_text 过滤器的文档: https://woocommerce.com/document/change-add-to-cart-button-text/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.