2

Hi Anyone knows how to change Pay button on checkout based on chosen payment method? I found something but I don't know if I could turn it into a snippet in function.php? Thank you.

    public function __construct() {
    $this->id = 'ry_ecpay_atm';
    $this->has_fields = false;
    $this->order_button_text = __('Pay via ATM', RY_WT::$textdomain);
    $this->method_title = __('ECPay ATM', RY_WT::$textdomain);
    $this->method_description = '';
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Hsin
  • 61
  • 1
  • 8

3 Answers3

6

This can be done with the following code (where you will set your payment gateway IDs and the corresponding desired button text):

add_filter('woocommerce_order_button_text', 'custom_order_button_text' );
function custom_order_button_text( $order_button_text ) {
    $default = __( 'Place order', 'woocommerce' ); // If needed
    // Get the chosen payment gateway (dynamically)
    $chosen_payment_method = WC()->session->get('chosen_payment_method');

    // Set your payment gateways IDs in EACH "IF" statement
    if( $chosen_payment_method == 'bacs'){
        // HERE set your custom button text
        $order_button_text = __( 'Bank wire payment', 'woocommerce' ); 
    } elseif( $chosen_payment_method == 'ry_ecpay_atm'){
        // HERE set your custom button text
        $order_button_text = __( 'Place order via ECPay', 'woocommerce' ); 
    }
    // jQuery code: Make dynamic text button "on change" event ?>
    <script type="text/javascript">
    (function($){
        $('form.checkout').on( 'change', 'input[name^="payment_method"]', function() {
            var t = { updateTimer: !1,  dirtyInput: !1,
                reset_update_checkout_timer: function() {
                    clearTimeout(t.updateTimer)
                },  trigger_update_checkout: function() {
                    t.reset_update_checkout_timer(), t.dirtyInput = !1,
                    $(document.body).trigger("update_checkout")
                }
            };
            t.trigger_update_checkout();
        });
    })(jQuery);
    </script><?php

    return $order_button_text;
}

Code goes in function.php file of your active child theme (or theme). Tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hi Thank you so much. It works but my problem is not solved. I got more payment method. How can I config the code? Does the code work with dropdown payment menu like this ?https://ibb.co/eE1GCx – Hsin Mar 21 '18 at 23:07
  • Thank you for the reminder. I only use the community couple time. I jus vote up and solve. Here is the code. It works really good except the dorpdown payment cannot be defined here, right? And the code is too long. – Hsin Mar 21 '18 at 23:25
3

I think this is an easier solution:

add_filter('woocommerce_available_payment_gateways', 'change_barion_label');
function change_barion_label($gateways) {
    if($gateways['ry_ecpay_atm']) {
        $gateways['ry_ecpay_atm']->order_button_text = 'new label';
    }
    return $gateways;
}

WooCommerce runs this filter when loading the payment gateways, so it should work site-wide.

passatgt
  • 4,234
  • 4
  • 40
  • 54
1
add_filter('woocommerce_order_button_text', 'custom_order_button_text' );
function custom_order_button_text( $order_button_text ) {
    $default = __( 'Place order', 'woocommerce' ); // If needed
    // Get the chosen payment gateway (dynamically)
    $chosen_payment_method = WC()->session->get('chosen_payment_method');

    ## --- For TESTING raw output on the chosen gateway ID --- ##
    // echo '<pre>' . $chosen_payment_method . '</pre>'; // <=== uncomment for testing

    // Set your payment gateways IDs in EACH "IF" statement
    if( $chosen_payment_method == 'bacs'){
        // HERE set your custom button text
        $order_button_text = __( 'Bank wire payment', 'woocommerce' ); 
       } elseif( $chosen_payment_method == 'ecpay_shipping_pay'){
        // HERE set your custom button text
        $order_button_text = __( 'Place order via Market', 'woocommerce' ); 
       } elseif( $chosen_payment_method == 'ecpay'){
        // HERE set your custom button text
        $order_button_text = __( 'Place order via ATM/Credit Card', 'woocommerce' ); 
     }
    // jQuery code: Make dynamic text button "on change" event ?>
    <script type="text/javascript">
    (function($){
        $('form.checkout').on( 'change', 'input[name^="payment_method"]', function() {
            var t = { updateTimer: !1,  dirtyInput: !1,
                reset_update_checkout_timer: function() {
                    clearTimeout(t.updateTimer)
                },  trigger_update_checkout: function() {
                    t.reset_update_checkout_timer(), t.dirtyInput = !1,
                    $(document.body).trigger("update_checkout")
                }
            };
            t.trigger_update_checkout();
        });
    })(jQuery);
    </script><?php

    return $order_button_text;
  }

and this is the payment in that dropdown.

'ecpay_payment_methods' => array(
            'title'     => __( 'Payment Method', 'ecpay' ),
            'type'      => 'multiselect',
            'description'   => __( 'Press CTRL and the right button on the mouse to select multi payments.', 'ecpay' ),
            'options'   => array(
                'Credit'    => $this->get_payment_desc('Credit'),
                'Credit_3'  => $this->get_payment_desc('Credit_3'),
                'Credit_6'  => $this->get_payment_desc('Credit_6'),
                'Credit_12'     => $this->get_payment_desc('Credit_12'),
                'Credit_18'     => $this->get_payment_desc('Credit_18'),
                'Credit_24'     => $this->get_payment_desc('Credit_24'),
                'WebATM'    => $this->get_payment_desc('WebATM'),
                'ATM'       => $this->get_payment_desc('ATM'),
                'CVS'       => $this->get_payment_desc('CVS'),
                'BARCODE'   => $this->get_payment_desc('BARCODE'),
                'ApplePay'  => $this->get_payment_desc('ApplePay')
            ),
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Hsin
  • 61
  • 1
  • 8
  • This dropdown is very unusual … so I don't know how to handle that as the code works with the payement gateway ID which is in this case 'ecpay' … you should need to add a `print_r(WC()->session->get('chosen_payment_method'));` to see what you get displayed when selecting different values (just for testing temporarilly) – LoicTheAztec Mar 21 '18 at 23:51
  • I should add the code in the plugin file and select the payment method from the dropdown menu and see what is printed out on the screen, is it? Thank you. – Hsin Mar 22 '18 at 01:15
  • Ok see it in your code HERE (I have updated it)… You will have to uncomment and save, just for testing and see what is the output when you make change on gateways + select fields. This way you will get the output… But I am not sure that your select fields will make something, as they are very custom and unusual… – LoicTheAztec Mar 22 '18 at 02:16
  • here is the print out result. https://ibb.co/hhjOaH I got "ecpay" from selecting either ATM or Credit cart. Does it mean the function won't work for dropdown? – Hsin Mar 22 '18 at 03:24
  • 1
    Yes because this is something very custom, that woocommerce can't handle… Woocommerce just handle payment gateway IDs (like they are displayed on Woocommerce settings > checkout, so in your case it's a kind of sub IDs handle specifically by the related payment gateway plugin… – LoicTheAztec Mar 22 '18 at 03:34
  • Thank you so much for your help. Sleep tight. :) – Hsin Mar 22 '18 at 03:55
  • Hi @LoicTheAztec How are you doing? Hope you are doing great. I got a little problem. How can I have the order button text change/ refresh when the customers change different payment on the checkout page? Thank you so much. – Hsin Apr 02 '18 at 11:03
  • See [**this threads**](https://stackoverflow.com/search?q=woocommerce_order_button_html) using `woocommerce_order_button_html` hook – LoicTheAztec Apr 02 '18 at 13:56
  • @LoicTheAztec Thank you and wish me luck. It seems a bit intimidating for non-programmer. I think it should be like inserting a paragraph of codes in the snippet, right? :) – Hsin Apr 03 '18 at 01:17