[英]WooCommerce - Change Add to Cart button text for products £40 or more
I am attempting to alter the "Add to Cart" button test on single product pages only when a product is £40 or more.仅当产品价格为 40 英镑或更多时,我才尝试更改单个产品页面上的“添加到购物车”按钮测试。 I have attempted the below snippet but it does not alter anything.
我尝试了下面的代码段,但它没有改变任何东西。
/**
* Change Add to Cart button text for products £40 or more
*
* @return string
*/
function banks_cart_button_text() {
global $product;
if( $product->get_regular_price >= '40' ) {
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woocommerce_custom_single_add_to_cart_text' );
function woocommerce_custom_single_add_to_cart_text() {
return __( 'Add to Cart with FREE UK Shipping', 'woocommerce' );
}
}
return $product->get_regular_price();
}
I was about to post and came across the below post which I updated but this also does not work.我正要发布并遇到了我更新的以下帖子,但这也不起作用。
Link: Change add to cart button and text based on WooCommerce product type Author: LoicTheAztec链接: 根据 WooCommerce 产品类型更改添加到购物车按钮和文本作者: LoicTheAztec
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product ) {
// Products above X amount
if( $product->get_regular_price >= '40' ) {
$button_text = __( "Add to Cart with FREE UK Shipping", "woocommerce" );
}
return '<a class="view-product button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
}
UPDATE:更新:
Adapted another snippet I used a while back but does not work.改编了我不久前使用的另一个片段,但不起作用。 Is the issue the this line:
是这一行的问题:
if( $product->get_regular_price >= '40' ) {
Snippet片段
// Replacing the single product button add to cart by a custom button when store is closed
add_action( 'woocommerce_single_product_summary', 'replace_single_add_to_cart_button', 1 );
function replace_single_add_to_cart_button() {
global $product, $url, $request ;
// Products equal to or more than X amount
if( $product->get_regular_price >= '40' ) {
// For variable product types (keeping attribute select fields)
if( $product->is_type( 'variable' ) ) {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
add_action( 'woocommerce_single_variation', 'custom_product_button', 20 );
}
// For all other product types
if( $product->is_type( 'simple' ) ) {
remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );
add_action( 'woocommerce_simple_add_to_cart', 'custom_product_button', 30 );
}
// For all other product types
else {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
add_action( 'woocommerce_simple_add_to_cart', 'custom_product_button', 30 );
}
}
else {
// Products equal to or more than X amount
if( $product->get_regular_price >= '40' ) {
// For variable product types (keeping attribute select fields)
if( $product->is_type( 'variable' ) ) {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
add_action( 'woocommerce_single_variation', 'custom_product_button', 20 );
}
// For all other product types
if( $product->is_type( 'simple' ) ) {
remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );
add_action( 'woocommerce_simple_add_to_cart', 'custom_product_button', 30 );
}
// For all other product types
else {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
add_action( 'woocommerce_simple_add_to_cart', 'custom_product_button', 30 );
}
}
}
}
function custom_product_button(){
// Display button
printf( '<a class="button disabled">%s</a>', __( "Add to Cart with FREE UK Shipping", "woocommerce" ) );
}
no need to place a function inside another.无需将功能放在另一个功能中。 this should be enough.
这应该足够了。
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woocommerce_custom_single_add_to_cart_text', 100, 2 );
function woocommerce_custom_single_add_to_cart_text( $add_to_cart_text, $product ) {
if ( $product->get_regular_price() >= '40' ) {
return __( 'Add to Cart with FREE UK Shipping', 'woocommerce' );
}
return $add_to_cart_text;
}
Try尝试
if( $product->get_regular_price() >= 40 ) {
if( $product->get_regular_price() >= 40 ) {
(brackets on the end and compare price against int not a string). (最后的括号并将价格与 int 进行比较,而不是字符串)。 Also check that you're getting a value.
还要检查您是否获得了价值。 Depending on your setup you may need to check for
根据您的设置,您可能需要检查
$product->get_sale_price()
$product->get_sale_price()
or或者
$product->get_price()
$product->get_price()
您可以使用上面的功能,或者使用 Locotranslate 插件,它为每种语言创建网站文本的模板(由基于单词来源的文件分隔,例如 woocommerce 文件、主题文件、插件文件),所以您只需将 woocommerce 文件的英文模板中的“添加到购物车”一词替换为您想要的文本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.