search

Home  >  Q&A  >  body text

Change the text of the "Pay Now" button on the WooCommerce checkout page to match the payment gateway option

<p>I've been trying for hours now and can't get this "basic" thing to work anyway. </p> <p>I have a bunch of payment gateways available and I need to include their names (including order total amount) in the "Pay Now" button text. </p> <p><strong>Example</strong>: "<code>Use Stripe to pay for order $49</code>"</p> <p>I have a piece of code that is said to automatically update the checkout when changing gateways.Please, can anyone help? </p> <pre class="brush:php;toolbar:false;">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 gateway $payment_method = WC()->session->get( 'chosen_payment_method' ); // Based on different gateways, display different button text (order button) if ( $payment_method == ' bacs ' ) { return sprintf( '%s %s', __('Place order and pay', 'woocommerce'), strip_tags( WC()->cart->get_total() ) . 'Use WireTransfer' ); } elseif ( $payment_method == ' check ' ) { return sprintf( '%s %s', __('Place order and pay', 'woocommerce'), strip_tags( WC()->cart->get_total() ) . 'Use personal check' ); } elseif ( $payment_method == ' cod ' ) { return sprintf( '%s %s', __('Place order and pay', 'woocommerce'), strip_tags( WC()->cart->get_total() ) . 'Cash on delivery' ); } elseif ( $payment_method == ' etco ' ) { return sprintf( '%s %s', __('Place order and pay', 'woocommerce'), strip_tags( WC()->cart->get_total() ) . 'Use EtCo' ); } else ( $payment_method == ' stripe ' ) { return sprintf( '%s %s', __('Place order and pay', 'woocommerce'), strip_tags( WC()->cart->get_total() ) . 'Use Stripe' ); } }</pre> <p><strong>Auto-update checkout script: </strong></p> <pre class="brush:php;toolbar:false;">add_action( 'wp_footer', 'reload_checkout_based_on_gateway_change', 999 ); function reload_checkout_based_on_gateway_change() { if ( is_checkout() && ! is_admin() ) { //End PHP and start SCRIPT ?> <script> ( function( $ ) { $( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() { $( 'body' ).trigger( 'update_checkout' ); } ); } ) (jQuery); </script> <?php } }</pre> <p><br /></p>
P粉395056196P粉395056196464 days ago744

reply all(1)I'll reply

  • P粉594941301

    P粉5949413012023-08-17 10:10:22

    There are a lot of errors in your code:

    • The main error is about strings: 'cheque' and 'cheque' are two different strings.
      So among all the if statements, none of the payment methods match.
    • Another error is that else does not support any conditional parameters.

    There are several ways to change the text of the checkout "Place Order" button:

    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' ); // 获取当前支付网关
            $cart_total_string = strip_tags( WC()->cart->get_total() ); // 获取订单总额字符串
            $pay_order_text    = __('Order &amp; Pay', 'woocommerce'); // 下单按钮
    
            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;
    }
    
    // 在支付方式更改时更新结账(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 you can also use the order_button_text attribute of WC_Payment_Gateway, as shown below:

    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() ); // 获取订单总额字符串
            $pay_order_text    = __('Order &amp; Pay', 'woocommerce'); // 下单按钮文本
    
            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;
    }
    

    Place the code in your child theme’s functions.php file (or in a plugin). It has been tested and works fine.

    reply
    0
  • Cancelreply