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

问题描述 投票:1回答:1

在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');
}

但现在我想将特定产品的按钮文本更改为(小写)“添加到购物车”。

一些帮助表示赞赏。

php wordpress button woocommerce product
1个回答
2
投票

以下代码将根据定义的产品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;
}

代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作。

© www.soinside.com 2019 - 2024. All rights reserved.