根据支付网关选项更改 WooCommerce 结帐上的“立即付款”按钮文本

问题描述 投票:0回答:1

我已经尝试了几个小时了,但我无法让这个“基本”的东西发挥作用。

我有很多可用的支付网关,我需要将其名称(包括订单总价值)包含在“立即付款”按钮文本中。

示例:“

Order & Pay $49 using Stripe

我的代码应该会在网关更改时自动更新结账。请问有人吗?

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 gateways
    $payment_method = WC()->session->get( 'chosen_payment_method' );

    // based on different gateways, display a different button text (place order button)
    if ( $payment_method == ' bacs ' ) {

            return sprintf( '%s %s', __('Order & Pay', 'woocommerce'), 
            strip_tags( WC()->cart->get_total() ) . ' using WireTransfer' );
        } 
    
    elseif ( $payment_method == ' cheque ' ) {

            return sprintf( '%s %s', __('Order & Pay', 'woocommerce'), 
            strip_tags( WC()->cart->get_total() ) . ' with a personal cheque' );
        } 

    elseif ( $payment_method == ' cod ' ) {

            return sprintf( '%s %s', __('Order & Pay', 'woocommerce'), 
            strip_tags( WC()->cart->get_total() ) . ' on delivery' );
        }

    elseif ( $payment_method == ' etco ' ) {

            return sprintf( '%s %s', __('Order & Pay', 'woocommerce'), 
            strip_tags( WC()->cart->get_total() ) . ' using EtCo' );
        }

    else ( $payment_method == ' stripe ' ) {

            return sprintf( '%s %s', __('Order & Pay', 'woocommerce'), 
            strip_tags( WC()->cart->get_total() ) . ' using Stripe' );
        }

}

“自动更新”结账脚本:

add_action( 'wp_footer', 'reload_checkout_based_on_gateway_change', 999 ); 
function reload_checkout_based_on_gateway_change() {

    if ( is_checkout() && ! is_admin() ) {

    // close PHP, get the SCRIPT going
    ?>

        <script>
        ( function( $ ) {
        $( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {

                $( 'body' ).trigger( 'update_checkout' );
            }
        );
      }
    )
    
     ( jQuery );

        </script>
    
        <?php
    }
}
php jquery woocommerce checkout payment-method
1个回答
0
投票

有很多错误...

主要错误在于字符串:

' cheque '
'cheque'
是两个不同的字符串。
因此,在您的所有 if 语句中,没有匹配的付款方式。

另一个是

else
不支持任何条件参数。

有多种方法可以更改结账“下订单”按钮文本:

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' ); // Get current payment gateways
        $cart_total_string = strip_tags( WC()->cart->get_total() ); // Get order total string
        $pay_order_text    = __('Order &amp; Pay', 'woocommerce'); // order button

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

// Update checkout on payment method change (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');
    });");
}

或者也使用

WC_Payment_Gateway
order_button_text
属性,例如:

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() ); // Get order total string
        $pay_order_text    = __('Order &amp; Pay', 'woocommerce'); // order button text

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

代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。

注意: 某些第三方支付网关不允许通过 WooCommerce 挂钩更改按钮文本。

© www.soinside.com 2019 - 2024. All rights reserved.