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

时间:2018-03-21 11:50:21

标签: php jquery wordpress woocommerce checkout

您好任何人都知道如何根据所选付款方式更改结账时的付款按钮?我找到了一些东西,但我不知道我是否可以将它变成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 = '';

3 个答案:

答案 0 :(得分:3)

可以使用以下代码(您将在其中设置支付网关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文件。经过测试并正常工作。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

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