根据 Woocommerce 选择的付款方式更改结帐时的付款按钮

Change Pay button on checkout based on Woocommerce chosen payment method

您好有人知道如何根据所选的付款方式更改结帐时的付款按钮吗?我找到了一些东西,但我不知道是否可以将它变成 function.php 中的片段?谢谢你。

    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 = '';

这可以通过以下代码完成(您将在其中设置您的支付网关 ID 和相应的所需按钮文本)

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;
}

代码进入您的活动子主题(或主题)的 function.php 文件。 已测试并有效。

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;
  }

这是该下拉列表中的付款。

'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')
            ),

我认为这是一个更简单的解决方案:

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 在加载支付网关时运行此过滤器,因此它应该在整个站点范围内工作。