更改WooCommerce结账页面上'立即付款”按钮的文本,以符合支付网关选项
P粉395056196
P粉395056196 2023-08-16 14:51:27
0
1
607
<p>我已经试了几个小时了,但是无论如何都无法让这个“基本”的东西正常工作。</p> <p>我有一堆可用的支付网关,我需要将其名称(包括订单总金额)包含在“立即支付”按钮文本中。</p> <p><strong>示例</strong>:“<code>使用Stripe支付订单$49</code>”</p> <p>我有一段代码,据说可以在更改网关时自动更新结账。请问,有人可以帮忙吗?</p> <pre class="brush:php;toolbar:false;">add_filter( 'woocommerce_order_button_text', 'order_button_text_based_on_gateway', 10, 1 ); function order_button_text_based_on_gateway( $cart ) { // 确保我们获取到了支付网关 $payment_method = WC()->session->get( 'chosen_payment_method' ); // 基于不同的网关,显示不同的按钮文本(下单按钮) if ( $payment_method == ' bacs ' ) { return sprintf( '%s %s', __('下单并支付', 'woocommerce'), strip_tags( WC()->cart->get_total() ) . ' 使用WireTransfer' ); } elseif ( $payment_method == ' cheque ' ) { return sprintf( '%s %s', __('下单并支付', 'woocommerce'), strip_tags( WC()->cart->get_total() ) . ' 使用个人支票' ); } elseif ( $payment_method == ' cod ' ) { return sprintf( '%s %s', __('下单并支付', 'woocommerce'), strip_tags( WC()->cart->get_total() ) . ' 货到付款' ); } elseif ( $payment_method == ' etco ' ) { return sprintf( '%s %s', __('下单并支付', 'woocommerce'), strip_tags( WC()->cart->get_total() ) . ' 使用EtCo' ); } else ( $payment_method == ' stripe ' ) { return sprintf( '%s %s', __('下单并支付', 'woocommerce'), strip_tags( WC()->cart->get_total() ) . ' 使用Stripe' ); } }</pre> <p><strong>“自动更新”结账脚本:</strong></p> <pre class="brush:php;toolbar:false;">add_action( 'wp_footer', 'reload_checkout_based_on_gateway_change', 999 ); function reload_checkout_based_on_gateway_change() { if ( is_checkout() && ! is_admin() ) { // 结束PHP,开始SCRIPT ?> <script> ( function( $ ) { $( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() { $( 'body' ).trigger( 'update_checkout' ); } ); } ) ( jQuery ); </script> <?php } }</pre> <p><br /></p>
P粉395056196
P粉395056196

全部回复(1)
P粉594941301

你的代码中有很多错误:

  • 主要错误是关于字符串:'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' ); // 获取当前支付网关
        $cart_total_string = strip_tags( WC()->cart->get_total() ); // 获取订单总额字符串
        $pay_order_text    = __('Order &amp; Pay', 'woocommerce'); // 下单按钮

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

// 在支付方式更改时更新结账(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_Gatewayorder_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() ); // 获取订单总额字符串
        $pay_order_text    = __('Order &amp; Pay', 'woocommerce'); // 下单按钮文本

        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文件中(或插件中)。经过测试,可以正常工作。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!