2

I need to change the order_button_text for a specific payment gateway (COD in this case).

I could only get it to change globally (for all payment gateways) using:

add_action( 'woocommerce_after_add_to_cart_button', 'multiple_orders_text' );
function woo_custom_order_button_text() {
    return __( 'Request Shipping Quote', 'woocommerce' );
}

But have found that if I add the line

$this->order_button_text  = __( 'Request a Quote', 'woocommerce' );

to the setup_properties() method in woocommerce/includes/gateways/cod/class-wc-gateway-cod.php it does work.

However this is clearly bad practice as I'm hacking a core plugin file.

How can I achieve this without hacking a woocommerce core file?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
jhob101
  • 432
  • 2
  • 6
  • 15

3 Answers3

10

You can do it like this:

add_filter( 'woocommerce_available_payment_gateways', 'woocommerce_available_payment_gateways' );
function woocommerce_available_payment_gateways( $available_gateways ) {
    if (! is_checkout() ) return $available_gateways;  // stop doing anything if we're not on checkout page.
    if (array_key_exists('paypal',$available_gateways)) {
        // Gateway ID for Paypal is 'paypal'. 
         $available_gateways['paypal']->order_button_text = __( 'Request a Quote', 'woocommerce' );
    }
    return $available_gateways;
}

reigelgallarde.me This code example is for paypal. For reference of the gateway IDs, please check WooCoomerce > Settings > Checkout > Gateway display order

reigelgallarde.me

Reigel Gallarde
  • 64,198
  • 21
  • 121
  • 139
  • Thanks loads, that worked! Made a small tweak to check the array key exists first, otherwise an errror is thrown: `if (array_key_exists("paypal",$available_gateways)) { $available_gateways['paypal']->order_button_text = __( 'Pay with Paypal', 'woocommerce' ); }` Hace chosen as accepted answer as I like that this solution works before the render cycle. – jhob101 Aug 18 '17 at 08:57
  • As a side question, where do you learn about these solutions? It's particularly with woocommerce that I seem to ended up scrabbling around for solutions in a somewhat undirected manner. – jhob101 Aug 18 '17 at 09:01
  • To be honest you can't find more help than reading the source code... tracing what this and that does. For your problem as an example, I started with looking for `order_button_text` and found it on the payment gateway class. I then look for the code where the payment gateway is being called, ended up on the function `woocommerce_checkout_payment`. Then saw that I can alter the variable `$available_gateways` there. I then look for the nearest filter I could find. Ended up `woocommerce_available_payment_gateways`. – Reigel Gallarde Aug 18 '17 at 09:08
  • PHP knowledge is a must too.. Because it's hard tracing code if you don't know how it works. – Reigel Gallarde Aug 18 '17 at 09:09
  • Thanks, that's helpful to know how you found the solution. Woo documentation isn't great. Cheers – jhob101 Aug 18 '17 at 13:10
2

2023 Summer Update

Has WooCommerce has changed, better use the following linked answer instead:
Change WooCommerce submit checkout button text based on the chosen payment method


Updated

Here it is the proper way to do it, using woocommerce_review_order_before_payment action hook with a custom function hooked in, using mainly jQuery (because it's a client side live event):

add_action( 'woocommerce_checkout_init', 'customizing_checkout_button' );
function customizing_checkout_button(){
    $text1  = __( 'Place order', 'woocommerce' );
    $text2  = __( 'Request a Quote', 'woocommerce' );

    wc_enqueue_js("var payment = 'input[name=payment_method]',
    pChecked = payment+':checked', placeOrder = '#place_order';
         
    function changeButtonPaymentText() {
        if($(pChecked).val() == 'cod' ) {
            $(placeOrder).html('{$text2}');
        } else {
            $(placeOrder).html('{$text1}');
        }
    }
    changeButtonPaymentText();
    $( 'form.checkout' ).on( 'change', payment, function() {
        changeButtonPaymentText();
    });");
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested and works on last WooCommerce version (7.9)

Useful: How to get the ID of a payment method in Woocommerce?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
-1

Easy solution, try to add the following code in your theme's function.php file.

/**
 * @snippet       Change checkout order button text
 * @package       WooCommerce
 */
function change_checkout_order_button_text() {
    return __( 'Complete Order', 'woocommerce' ); 
}
add_filter( 'woocommerce_order_button_text', 'change_checkout_order_button_text' );