I am attempting to alter the "Add to Cart" button test on single product pages only when a product is £40 or more. 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
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" ) );
}
3
Answers
Try
(brackets on the end and compare price against int not a string).
Also check that you’re getting a value.
Depending on your setup you may need to check for
or
no need to place a function inside another. this should be enough.
You can use the functions above, or by using Locotranslate plugin, it creates templates for the site’s texts (separated by files based on the origin of the word, for example woocommerce file, theme file, plugins file) for each language, so you just replace the "add to cart" word in the english language template of woocommerce file with the text you want.