Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

php - Change Pay button on checkout based on Woocommerce chosen payment method

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 = '';
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...