简体   繁体   English

更改 Woocommerce 中特定产品的添加到购物车按钮文本

[英]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.如果您想在没有编码的情况下更改特定产品的添加到购物车按钮文本,这里是解决方案。

  1. .postid-1324 => Product id .postid-1324 => 产品 ID
  2. .single_add_to_cart_button => add to cart button class .single_add_to_cart_button => 添加到购物车按钮类

 .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.

相关问题 如何在 WooCommerce 的单页上更改特定变量产品的添加到购物车按钮文本 - How to change add to cart button text for specific variable products on single page in WooCommerce 如果用户在 WooCommerce 中购买了特定产品,则更改“添加到购物车”文本 - Change “add to cart” text if a user has bought specific products in WooCommerce WooCommerce - 更改 40 英镑或更多产品的“添加到购物车”按钮文本 - WooCommerce - Change Add to Cart button text for products £40 or more WooCommerce:更改多个产品的添加到购物车按钮文本,并重定向到结帐 - WooCommerce: Change add to cart button text for multiple products with a redirection to checkout 禁用特定 WooCommerce 产品的添加到购物车按钮 - Disabling Add to Cart Button for Specific WooCommerce Products 在WooCommerce中为特定产品添加额外添加到购物车按钮 - Add extra add to cart button to specific products in WooCommerce 更改 WooCommerce 添加到特定标签的购物车文本 - Change WooCommerce add to cart text for specific tag 在Woocommerce商店页面中更改用于简单产品的购物车按钮 - Change add-to-cart button for simple products in Woocommerce shop page 更改woocommerce中特定产品的购物车项目数量 - Change the cart item quantity for specific products in woocommerce Woocommerce添加到购物车产品仅限4种特定产品 - Woocommerce Add to cart product Restriction to 4 specific products
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM