skip to Main Content

I have tried for hours now, and I cannot get this "basic" thing working whatsoever.

I have a bunch of payment gateways available and I need the name of it, including the total order value, to be included in the "Pay Now" button text.

Example: "Order & Pay $49 using Stripe"

I have code that supposedly should auto-update the checkout on gateway change. Please, someone?

add_filter( 'woocommerce_order_button_text', 'order_button_text_based_on_gateway', 10, 1 );
function order_button_text_based_on_gateway( $cart ) {

    // make sure we get the payment gateways
    $payment_method = WC()->session->get( 'chosen_payment_method' );

    // based on different gateways, display a different button text (place order button)
    if ( $payment_method == ' bacs ' ) {

            return sprintf( '%s %s', __('Order & Pay', 'woocommerce'), 
            strip_tags( WC()->cart->get_total() ) . ' using WireTransfer' );
        } 
    
    elseif ( $payment_method == ' cheque ' ) {

            return sprintf( '%s %s', __('Order & Pay', 'woocommerce'), 
            strip_tags( WC()->cart->get_total() ) . ' with a personal cheque' );
        } 

    elseif ( $payment_method == ' cod ' ) {

            return sprintf( '%s %s', __('Order & Pay', 'woocommerce'), 
            strip_tags( WC()->cart->get_total() ) . ' on delivery' );
        }

    elseif ( $payment_method == ' etco ' ) {

            return sprintf( '%s %s', __('Order & Pay', 'woocommerce'), 
            strip_tags( WC()->cart->get_total() ) . ' using EtCo' );
        }

    else ( $payment_method == ' stripe ' ) {

            return sprintf( '%s %s', __('Order & Pay', 'woocommerce'), 
            strip_tags( WC()->cart->get_total() ) . ' using Stripe' );
        }

}

The "auto update" checkout script:

add_action( 'wp_footer', 'reload_checkout_based_on_gateway_change', 999 ); 
function reload_checkout_based_on_gateway_change() {

    if ( is_checkout() && ! is_admin() ) {

    // close PHP, get the SCRIPT going
    ?>

        <script>
        ( function( $ ) {
        $( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {

                $( 'body' ).trigger( 'update_checkout' );
            }
        );
      }
    )
    
     ( jQuery );

        </script>
    
        <?php
    }
}

2

Answers


  1. There are a lot of mistakes in your code:

    • The main mistake is about strings: ' cheque ' and 'cheque' are two different strings.
      So in all your if statements, no payment method was matching.
    • The other one is that else doesn’t support any conditional arguments.

    There is multiple ways to Change checkout "Place order" Button Text:

    add_filter( 'woocommerce_order_button_text', 'order_button_text_based_on_gateway', 10 );
    function order_button_text_based_on_gateway( $button_text ) {
        if ( is_checkout() && ! is_wc_endpoint_url() ) {
            $payment_method    = WC()->session->get( 'chosen_payment_method' ); // Get current payment gateways
            $cart_total_string = strip_tags( WC()->cart->get_total() ); // Get order total string
            $pay_order_text    = __('Order &amp; Pay', 'woocommerce'); // order button
    
            if ( $payment_method == 'bacs' ) {
                $payment_method_text = __('using WireTransfer', 'woocommerce');
            } 
            elseif ( $payment_method == 'cheque' ) {
                $payment_method_text = __('with a personal cheque', 'woocommerce');
            } 
            elseif ( $payment_method == 'cod' ) {
                $payment_method_text = __('on delivery', 'woocommerce');
            }
            elseif ( $payment_method == 'etco' ) {
                $payment_method_text = __('using EtCo', 'woocommerce');
            }
            elseif ( $payment_method == 'stripe' ) {
                $payment_method_text = __('using Stripe', 'woocommerce');
            }
    
            if ( isset($payment_method_text) ) {
                $button_text = sprintf( '%s %s %s', $pay_order_text, $cart_total_string, $payment_method_text );
            }
        }
        return $button_text;
    }
    
    // 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');
        });");
    }
    

    Or also using the WC_Payment_Gateway order_button_text property like:

    add_filter('woocommerce_available_payment_gateways', 'change_payment_text_button');
    function change_payment_text_button( $payment_gateways ) {
        if ( is_checkout() && ! is_wc_endpoint_url() ) {
            $cart_total_string = strip_tags( WC()->cart->get_total() ); // Get order total string
            $pay_order_text    = __('Order &amp; Pay', 'woocommerce'); // order button text
    
            if ( isset($payment_gateways['bacs']) ) {
                $payment_gateways['bacs']->order_button_text = sprintf( '%s %s %s', 
                    $pay_order_text, $cart_total_string, __('using WireTransfer', 'woocommerce') );
            } 
            if ( isset($payment_gateways['cheque']) ) {
                $payment_gateways['cheque']->order_button_text = sprintf( '%s %s %s', 
                    $pay_order_text, $cart_total_string, __('with a personal cheque', 'woocommerce') );
            } 
            if ( isset($payment_gateways['cod']) ) {
                $payment_gateways['cod']->order_button_text = sprintf( '%s %s %s', 
                    $pay_order_text, $cart_total_string, __('on delivery', 'woocommerce') );
            }
            if ( isset($payment_gateways['etco']) ) {
                $payment_gateways['etco']->order_button_text = sprintf( '%s %s %s', 
                    $pay_order_text, $cart_total_string, __('using EtCo', 'woocommerce') );
            }
            if ( isset($payment_gateways['stripe']) ) {
                $payment_gateways['stripe']->order_button_text = sprintf( '%s %s %s', 
                    $pay_order_text, $cart_total_string, __('using Stripe', 'woocommerce') );
            }
        }
        return $payment_gateways;
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). Tested and works.

    Note: Some third party payment gateways doesn’t allow to change the button text via WooCommerce hooks.

    Login or Signup to reply.
  2. Dont work!
    I put it into my child theme, nothing changed or happened

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search