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

WooCommerce购买后多重跳转及订单追加商品实现咨询

WooCommerce 购买后多重跳转及订单追加商品实现方案

一、基础下单后跳转至自定义追加页面

基于你已有基础跳转代码,这里优化为支持后续追加逻辑的版本,通过会话记录当前订单ID,确保跳转后能关联到目标订单:

// 结账完成后跳转至自定义追加商品页面
add_filter('woocommerce_checkout_redirect', 'redirect_to_add_on_page_after_checkout');
function redirect_to_add_on_page_after_checkout($redirect_url) {
    $order_id = isset($_GET['order-received']) ? absint($_GET['order-received']) : 0;
    
    if ($order_id > 0) {
        // 将订单ID存入会话,供后续页面调用
        WC()->session->set('pending_add_order', $order_id);
        // 替换为你的自定义追加页面URL
        $redirect_url = home_url('/add-on-products/');
    }
    
    return $redirect_url;
}

二、自定义页面实现追加商品功能

在自定义页面中添加商品选择表单,并处理提交逻辑,将选中的商品追加到已完成订单:

1. 自定义页面的商品选择表单(嵌入页面模板)

<form method="post" action="">
    <select name="product_id" required>
        <option value="">选择附加商品</option>
        <!-- 替换为你的附加商品ID和名称 -->
        <option value="123">扩展服务A - ¥99</option>
        <option value="456">配件B - ¥49</option>
    </select>
    <input type="number" name="quantity" value="1" min="1" required>
    <button type="submit" name="add_to_order">添加到我的订单</button>
</form>

2. 表单提交处理逻辑

// 处理附加商品添加请求
add_action('template_redirect', 'handle_add_on_product_submission');
function handle_add_on_product_submission() {
    if (is_page('add-on-products') && isset($_POST['add_to_order'])) {
        $order_id = WC()->session->get('pending_add_order');
        if (!$order_id) {
            wp_redirect(home_url('/my-account/orders/'));
            exit;
        }

        $order = wc_get_order($order_id);
        // 验证订单有效性及用户权限
        if (!$order || $order->get_user_id() !== get_current_user_id() || $order->get_status() === 'cancelled') {
            WC()->session->__unset('pending_add_order');
            wp_redirect(home_url('/my-account/orders/'));
            exit;
        }

        // 获取提交的商品信息
        $product_id = absint($_POST['product_id']);
        $quantity = absint($_POST['quantity']) ?: 1;
        $product = wc_get_product($product_id);

        if (!$product) {
            wc_add_notice('无效的附加商品', 'error');
            return;
        }

        // 添加商品到订单并重新计算总额
        if ($order->add_product($product, $quantity)) {
            $order->calculate_totals();
            // 可选:更新订单状态,比如设置为待付款(若需用户支付追加金额)
            $order->update_status('on-hold', '已添加附加商品,等待支付追加款项');
            
            // 清除会话标记,完成追加流程
            WC()->session->__unset('pending_add_order');
            
            // 跳转至订单支付页(若需支付)或订单详情页
            wp_redirect($order->get_checkout_payment_url());
            // 无需支付时跳转至订单详情:wp_redirect($order->get_view_order_url());
            exit;
        } else {
            wc_add_notice('添加附加商品失败,请重试', 'error');
        }
    }
}

三、多重跳转逻辑控制

  • 首次跳转:结账完成 → 自定义追加商品页面
  • 二次跳转:提交追加商品 → 订单支付页/订单详情页
  • 无操作自动跳转:在自定义页面添加JS倒计时,超时后自动返回订单列表:
// 5分钟无操作自动跳转回订单列表
setTimeout(() => {
    window.location.href = '/my-account/orders/';
}, 300000);

四、关键注意事项

  • 确保WooCommerce会话功能正常,若使用缓存插件,需排除woocommerce_session_开头的Cookie,避免会话丢失。
  • 自定义页面需添加权限校验,防止非订单所有者访问(代码已包含用户ID验证)。
  • 若追加商品需要额外支付,需确保订单支付链接能正确计算剩余待付款金额;无需支付则直接更新订单总额即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 11:17:28