2

I tried Change checkout submit button text for a specific payment method in WooCommerce answer code, but no changes were applied to the checkout submit button text.

My target is that by selecting any method, the text will change to the same method or the desired text "for example, if "cod" (cash on delivery) is selected or "cheque" or "bacs" or... the button text will also change to a different text.

Any help is appreciated.


Update:

Then I tried the updated answer (same link), it was working for 2 seconds and the text was changed back to default one.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • thank you Unfortunately, after a very short pause, the text changes again to its default text, i.e. place order text. Is the problem from my WooCommerce side? – Brad Harley Jul 20 '23 at 17:24
  • 1
    I have finally answered here. This will work for you. Maybe you don't need the first function, as it seems that in your case, the checkout already updated on payment method change. – LoicTheAztec Jul 20 '23 at 18:17

1 Answers1

3

I found a much cleaner way using PHP and a bit of jQuery to "update_checkout" on payment method change. This will work on all cases, so it will work for you too:

// Update checkout on payment method change (jQuery)
add_action( 'woocommerce_checkout_init', 'trigger_update_checkout_on_payment_method_change' );
function trigger_update_checkout_on_payment_method_change(){
    wc_enqueue_js("$('form.checkout').on( 'change', 'input[name=payment_method]', function(){
        $(document.body).trigger('update_checkout');
    });");
}

// Change the displayed text on the checkout submit button based on payment methods
add_filter( 'woocommerce_order_button_html', 'custom_place_order_button_html', 900 );
function custom_place_order_button_html( $button_html ) {
    $original_text  = __( 'Place order', 'woocommerce' ); // Default button text
    $custom_text    = __( 'Request a Quote', 'woocommerce' ); // For "COD" payment method

    if( WC()->session->get('chosen_payment_method') === 'cod' && ! is_wc_endpoint_url() ) {
        $button_html = str_replace( $original_text, $custom_text, $button_html );
    }
    return $button_html;
}

Code goes in your child theme's functions.php file or in a plugin file.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399