You need to enable JavaScript to run this app. WooCommerce点击结账按钮弹出模态框 自定义支付插件展示二维码实现咨询
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce点击结账按钮弹出模态框 自定义支付插件展示二维码实现咨询

WooCommerce自定义支付插件二维码弹窗实现方案

核心问题答复

  • 需要拦截WooCommerce的结账提交事件,但不需要单独捕获结账按钮的点击动作,直接监听WooCommerce内置的checkout_place_order事件即可,默认的process_payment执行逻辑会触发页面跳转,不符合弹窗展示二维码的需求。

具体实现步骤

1. 调整后端process_payment方法返回格式

修改原有方法,把返回值从默认的重定向配置改为JSON格式,给前端返回二维码数据:

public function process_payment() {
    // 你原有生成订单、校验参数的逻辑保留
    global $woocommerce;
    $order = wc_get_order( $order_id ); // 补充你自己的订单ID获取逻辑

    try {
        $response = json_decode($this->getQr('url'));
    }
    catch (Exception $e) {
        wp_send_json_error([
            'message' => $e->getMessage()
        ]);
    }

    $url = $response->url;
    $qr  = $response->qr_code;
    
    // 成功场景返回前端需要的所有数据
    wp_send_json_success([
        'qr_code' => $qr,
        'pay_url' => $url,
        'order_id' => $order->get_id(),
        'success_url' => $this->get_return_url( $order )
    ]);
}

2. 前端添加自定义JS拦截提交逻辑

在插件中入队自定义JS文件,监听结账表单的提交事件,收到后端返回的二维码后渲染弹窗:

jQuery(function($) {
    // 监听WooCommerce内置的结账提交事件
    $('form.woocommerce-checkout').on('checkout_place_order', function(e) {
        // 只拦截当前自定义支付方式的提交
        if ($('#payment_method_你的支付方式标识').is(':checked')) {
            e.preventDefault();
            // 提交表单数据到WooCommerce结账接口
            $.post(wc_checkout_params.checkout_url, $(this).serialize(), function(res) {
                if (res.result === 'success') {
                    // 渲染二维码弹窗,可自行调整样式
                    let modalHtml = `
                    <div class="qr-pay-mask" style="position: fixed;inset: 0;background: rgba(0,0,0,.7);z-index: 99998;"></div>
                    <div class="qr-pay-modal" style="position: fixed;top: 50%;left: 50%;transform: translate(-50%,-50%);background: #fff;padding: 24px;border-radius: 8px;z-index: 99999;">
                        <h4>请使用对应APP扫码支付</h4>
                        <img src="${res.qr_code}" alt="支付二维码" style="width: 240px;height: 240px;display: block;margin: 16px auto;">
                        <p class="tip">支付完成后会自动跳转,也可<a href="${res.pay_url}" target="_blank">手动查询支付结果</a></p>
                        <button class="close-qr-modal" style="margin-top: 12px;width: 100%;padding: 8px;">关闭</button>
                    </div>
                    `;
                    $('body').append(modalHtml);
                    
                    // 绑定弹窗关闭事件
                    $('.close-qr-modal, .qr-pay-mask').on('click', function() {
                        $('.qr-pay-mask, .qr-pay-modal').remove();
                    });

                    // 可选:添加支付状态轮询,支付成功后自动跳转到成功页
                    let timer = setInterval(function() {
                        $.post('你的后端查询订单状态接口地址', {order_id: res.order_id}, function(pollRes) {
                            if (pollRes.status === 'paid') {
                                clearInterval(timer);
                                window.location.href = res.success_url;
                            }
                        });
                    }, 3000);
                } else {
                    // 错误场景展示WC原生错误提示
                    $('.woocommerce-notices-wrapper').html(res.messages);
                }
            });
            return false;
        }
    });
});

3. 补充注意事项

  • 代码里的你的支付方式标识需要替换为你注册支付网关时设置的ID,否则拦截逻辑不会生效
  • 后端需要额外新增一个查询订单支付状态的接口,供前端轮询调用,注意做好权限校验
  • 弹窗样式可以根据站点主题自行调整,避免和现有样式冲突

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.18 13:36:10