如何为多商品设置相同order id、invoice并统一payment method?
同发票号订单合并展示&统一支付实现方案
核心逻辑是按发票号(invoice_no)对订单数据分组聚合,统计总价、总数量,同时聚合同发票下的所有订单ID用于统一支付,具体修改如下:
现有问题对应的页面效果截图:
1. 修改SQL查询逻辑
将原逐行查询所有订单的逻辑,调整为按invoice_no分组,对金额、数量做聚合,同时收集同发票下的所有订单ID、规格等信息:
SELECT invoice_no, SUM(due_amount) AS total_due, SUM(qty) AS total_qty, GROUP_CONCAT(DISTINCT size SEPARATOR '、') AS size_list, GROUP_CONCAT(DISTINCT order_id SEPARATOR ',') AS order_ids, MIN(order_date) AS order_date, order_status FROM customer_orders WHERE customer_id='$customer_id' GROUP BY invoice_no, order_status ORDER BY order_date DESC
2. 完整修改后的页面代码
对应调整循环渲染逻辑、支付链接参数:
<div class="table-responsive"> <table class="table table-bordered table-hover"> <thead> <tr> <th>序号</th> <th>应付总金额</th> <th>发票编号</th> <th>总数量</th> <th>规格</th> <th>下单日期</th> <th>支付状态</th> <th>操作</th> </tr> </thead> <tbody> <?php $customer_session = $_SESSION['customer_email']; $get_customer = "select * from customers where customer_email='$customer_session'"; $run_customer = mysqli_query($con,$get_customer); $row_customer = mysqli_fetch_array($run_customer); $customer_id = $row_customer['customer_id']; // 替换为分组聚合的查询语句 $get_orders = "SELECT invoice_no, SUM(due_amount) AS total_due, SUM(qty) AS total_qty, GROUP_CONCAT(DISTINCT size SEPARATOR '、') AS size_list, GROUP_CONCAT(DISTINCT order_id SEPARATOR ',') AS order_ids, MIN(order_date) AS order_date, order_status FROM customer_orders WHERE customer_id='$customer_id' GROUP BY invoice_no, order_status ORDER BY order_date DESC"; $run_orders = mysqli_query($con,$get_orders); $i = 0; while($row_orders = mysqli_fetch_array($run_orders)){ $order_ids = $row_orders['order_ids']; $total_due = $row_orders['total_due']; $invoice_no = $row_orders['invoice_no']; $total_qty = $row_orders['total_qty']; $size_list = $row_orders['size_list']; $order_date = substr($row_orders['order_date'],0,11); $order_status = $row_orders['order_status']; $i++; if($order_status == 'Pending'){ $order_status = '未支付'; }else{ $order_status = '已支付'; } ?> <tr> <th> <?php echo $i; ?> </th> <td> AED<?php echo $total_due; ?> </td> <td> <?php echo $invoice_no; ?> </td> <td> <?php echo $total_qty; ?> </td> <td> <?php echo $size_list; ?> </td> <td> <?php echo $order_date; ?> </td> <td> <?php echo $order_status; ?> </td> <td> <a href="confirm.php?order_ids=<?php echo $order_ids; ?>" target="_blank" class="btn btn-primary btn-sm"> 确认支付 </a> </td> </tr> <?php } ?> </tbody> </table> </div>
3. 配套调整confirm.php逻辑
原有逻辑是处理单个order_id的支付,现在只需接收逗号分隔的order_ids参数,拆分后批量更新所有对应订单的支付状态,即可实现多订单统一支付。
内容的提问来源于stack exchange,提问作者Sean Cruz
相关产品推荐
相关产品推荐

