简体   繁体   中英

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. 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" ) );
}

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 ) {

(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()

您可以使用上面的功能,或者使用 Locotranslate 插件,它为每种语言创建网站文本的模板(由基于单词来源的文件分隔,例如 woocommerce 文件、主题文件、插件文件),所以您只需将 woocommerce 文件的英文模板中的“添加到购物车”一词替换为您想要的文本。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM