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

Next.js项目中上衣促销价格计算错误问题求助

促销活动:3件上衣1000EGP的价格计算逻辑修复

促销规则回顾

  • 2件上衣:按原价计算
  • 3件上衣:统一收取1000EGP
  • 4件上衣:3件按1000EGP + 1件按原价
  • 6件上衣:统一收取2000EGP,以此类推

原代码核心错误

  1. 数量计算错误:用tops.length(购物车中上衣商品项的个数)代替实际购买的上衣总数量,比如1个商品项包含2件上衣,会被错误当成1件处理。
  2. 单价取值错误:直接用tops[0].price作为剩余上衣的单价,若购物车中有不同价格的上衣,会导致计算偏差。
  3. 总价修正逻辑错误:cartTotal -= discountedTotal的写法完全错误,原cartTotal已经包含了所有上衣的原价,正确逻辑应该是替换上衣的原价总和为折扣后的总价。

修正后的代码实现(默认上衣单价相同)

async function getProducts() {
  let cartTotal = 0;

  const productPromises = cart.map(async (item) => {
    const product = await getProduct(item._id);
    cartTotal += product.price * item.quantity;
    return {
      ...product,
      quantity: item.quantity,
      color: item.color,
      size: item.size,
    };
  });

  const productDetails = await Promise.all(productPromises);

  if (offer3Tops) {
    const tops = productDetails.filter(product => product.category[0] === 'Tops');
    if (tops.length === 0) return; // 无上衣商品,直接跳过

    // 计算上衣总购买数量
    const totalTops = tops.reduce((sum, product) => sum + product.quantity, 0);
    // 计算上衣原价总和
    const originalTopsTotal = tops.reduce((sum, product) => sum + product.price * product.quantity, 0);

    // 计算可享受折扣的组数和剩余数量
    const discountedSets = Math.floor(totalTops / 3);
    const remainingTopsCount = totalTops % 3;

    // 计算折扣后的上衣总价(假设所有上衣单价相同)
    const singleTopPrice = tops[0].price;
    const discountedTopsTotal = discountedSets * 1000 + remainingTopsCount * singleTopPrice;

    // 替换上衣原价为折扣价,修正购物车总价
    cartTotal = cartTotal - originalTopsTotal + discountedTopsTotal;
  }

  setProducts(productDetails);
  setCartTotal(cartTotal);
  setOrderTotal(Math.ceil(cartTotal + shipping));
  setLoading(false);
}

扩展:上衣单价不同的处理方案

如果购物车中存在不同价格的上衣,建议优先将高价上衣纳入折扣组,最大化用户优惠,代码调整如下:

async function getProducts() {
  let cartTotal = 0;

  const productPromises = cart.map(async (item) => {
    const product = await getProduct(item._id);
    cartTotal += product.price * item.quantity;
    return {
      ...product,
      quantity: item.quantity,
      color: item.color,
      size: item.size,
    };
  });

  const productDetails = await Promise.all(productPromises);

  if (offer3Tops) {
    const tops = productDetails.filter(product => product.category[0] === 'Tops');
    if (tops.length === 0) return;

    // 按上衣单价从高到低排序,优先用高价商品凑折扣组
    const sortedTops = [...tops].sort((a, b) => b.price - a.price);
    const totalTops = tops.reduce((sum, product) => sum + product.quantity, 0);
    const originalTopsTotal = tops.reduce((sum, product) => sum + product.price * product.quantity, 0);

    let discountedTopsTotal = 0;
    let remainingTops = totalTops;

    // 先处理折扣组
    const discountedSets = Math.floor(remainingTops / 3);
    discountedTopsTotal += discountedSets * 1000;
    remainingTops -= discountedSets * 3;

    // 处理剩余上衣,按原价计算
    for (const product of sortedTops) {
      if (remainingTops <= 0) break;
      const useQty = Math.min(product.quantity, remainingTops);
      discountedTopsTotal += useQty * product.price;
      remainingTops -= useQty;
    }

    // 修正购物车总价
    cartTotal = cartTotal - originalTopsTotal + discountedTopsTotal;
  }

  setProducts(productDetails);
  setCartTotal(cartTotal);
  setOrderTotal(Math.ceil(cartTotal + shipping));
  setLoading(false);
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 05:35:56