无法计算报价(Next.js)(JavaScript)

0kjbasz6  于 5个月前  发布在  Java
关注(0)|答案(1)|浏览(63)

所以我的客户想创建一个报价,所以每3个顶部是1000 EGP。所以如果我有2,那么它的正常价格3它的1000 4是1000为3和1为正常价格6为2000等。

if (offer3Tops) {
  const tops = productDetails.filter((product) => product.category[0] === 'Tops');
  const setsOf3 = Math.ceil(tops.length / 3);
  const remainingTops = tops.length % 3;

  const discountedTotal = setsOf3 * 1000 + remainingTops * tops[0].price;

  cartTotal -= discountedTotal;
}

字符串
尝试使用ceil,但它四舍五入,所以2是1000等。尝试使用floor,但我得到900每套而不是1000。
验证码:

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');
          const discountedSets = Math.floor(tops.length / 3) 
          const remainingTops = tops.length % 3;
          console.log(discountedSets, remainingTops);

          const discountedTotal = discountedSets * 1000 + remainingTops * tops[0].price;
          console.log(discountedTotal);
          cartTotal -= discountedTotal;
        }
      
        setProducts(productDetails);
        setCartTotal(cartTotal);
        setOrderTotal(Math.ceil(cartTotal + shipping ));
        setLoading(false);
      }
  
      async function getCities() {
          const statesApi = await getAllStates();
          setStates(statesApi);
      }
  
      getCities();
      getProducts();
  }, [cart, shipping]);

dy2hfwbg

dy2hfwbg1#

解决了:

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');
    const discountedSets = Math.floor(tops.reduce((total, product) => total + product.quantity, 0) / 3);
    const remainingTops = tops.reduce((total, product) => total + product.quantity, 0) % 3;

    const discountedTotal = discountedSets * 1000 + remainingTops * tops[0].price;
    console.log(discountedSets, remainingTops, discountedTotal);
    cartTotal -= discountedTotal;
  }

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

async function getCities() {
  const statesApi = await getAllStates();
  setStates(statesApi);
}

getCities();
getProducts();

字符串
还得计算数量。

相关问题