You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于支付网关修改WooCommerce结账页面“立即支付”按钮文本

问题描述

折腾了好几个小时,这个“基础”功能就是跑不起来。我的站点支持多个支付网关,需要把支付网关名称和订单总金额整合到“立即支付”按钮文本里,比如: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
    }
}

问题排查与修复

核心问题点

  1. 过滤器参数错误:woocommerce_order_button_text的回调参数是默认按钮文本$button_text,不是$cart,参数写错导致逻辑混乱。
  2. 支付方法ID带空格:判断条件里的支付方法值(比如' bacs ')带前后空格,但WooCommerce会话中存储的支付方法ID是无空格的(比如'bacs'),导致匹配失败。
  3. else语法错误:最后一个else后面加了条件,这是语法错误,else不能带条件,应改为elseif或直接用else兜底。
  4. JS语法错误:自执行函数写法错误,}(jQuery));的格式不对,导致脚本无法正常运行。

修复后的完整代码

支付按钮文本过滤代码

add_filter( 'woocommerce_order_button_text', 'order_button_text_based_on_gateway', 10, 1 );
function order_button_text_based_on_gateway( $button_text ) {
    // 获取选中的支付方式
    $payment_method = WC()->session->get( 'chosen_payment_method' );
    
    // 基础文本模板
    $base_text = sprintf( '%s %s', __('Order & Pay', 'woocommerce'), strip_tags( WC()->cart->get_total() ) );

    // 按支付方式匹配文本
    switch( $payment_method ) {
        case 'bacs':
            return $base_text . ' using WireTransfer';
        case 'cheque':
            return $base_text . ' with a personal cheque';
        case 'cod':
            return $base_text . ' on delivery';
        case 'etco':
            return $base_text . ' using EtCo';
        case 'stripe':
            return $base_text . ' using Stripe';
        default:
            return $button_text; // 保留默认按钮文本
    }
}

结账页面更新脚本

add_action( 'wp_footer', 'reload_checkout_based_on_gateway_change', 999 ); 
function reload_checkout_based_on_gateway_change() {
    // 仅在结账页面(非订单完成等端点)加载脚本
    if ( is_checkout() && ! is_admin() && ! is_wc_endpoint_url() ) {
        ?>
        <script>
        jQuery(function($) {
            $('form.checkout').on('change', 'input[name^="payment_method"]', function() {
                $(document.body).trigger('update_checkout');
            });
        });
        </script>
        <?php
    }
}

内容的提问来源于stack exchange,提问作者Learning WooStoreFront

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 00:22:40