Home > Mobile >  WooCommerce - Change Add to Cart button text for products £40 or more
WooCommerce - Change Add to Cart button text for products £40 or more

Time:06-18

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  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 >%s</a>', __( "Add to Cart with FREE UK Shipping", "woocommerce" ) );
}

CodePudding user response:

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;
    }

CodePudding user response:

Try

if( $product->get_regular_price() >= 40 ) {

(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

$product->get_sale_price()

or

$product->get_price()

  • Related