简体   繁体   English

如何将不同尺寸的相同产品作为单独的商品添加到购物车中?

[英]How to add the same product with different sizes to the shopping cart as a separate item?

I am making a cake shop where a user can add a cake to Cart.我正在做一个蛋糕店,用户可以在其中将蛋糕添加到购物车。 While in the cart, they can change the cake size to what that they want.在购物车中,他们可以将蛋糕大小更改为他们想要的大小。 On changing the cake size, the price changes and gets stored in the session在更改蛋糕大小时,价格会发生变化并存储在 session 中
在此处输入图像描述

I realized that if the user adds the same cake to the cart, it overrides the one previously added.我意识到如果用户将相同的蛋糕添加到购物车中,它会覆盖之前添加的蛋糕。
This is not what I want, as I'd like to add a new cake to the cart if the cake size of the cake currently in the cart is not the same.这不是我想要的,因为如果当前购物车中蛋糕的蛋糕大小不同,我想将新蛋糕添加到购物车中。

I'm storing the cart in a session based on this model:我将购物车存储在基于此 model 的 session 中:

module.exports = function Cart(oldCart) {

    // If there's no cart in the session, create a new empty object
    this.items = oldCart.items || {};
    this.totalQty = oldCart.totalQty || 0;
    this.totalPrice = oldCart.totalPrice || 0;

    //item will be an object based on a document from MongoDB
    //id is the unique _id from the item
    this.add = function(item, id) {
        let storedItem = this.items[id];

        //If the item is not on the cart, add it
        if(!storedItem){
            storedItem = this.items[id] = {item: item, qty: 0, price: 0, cakesize: '1kg', singlePrice: 0}
        }

        //If the cart is on the cart, increment its quantity and price
        storedItem.qty ++;
        storedItem.price = storedItem.item.price['1000'] * storedItem.qty;

        //Total of all items in the cart
        this.totalQty++;
        this.totalPrice += storedItem.item.price['1000'];
    }

    //Function to create an array
    this.generateArray = function() {
        let arr = [];
        for (var id in this.items) {
            arr.push(this.items[id]);
        }
        return arr;
    }

};

The controller to render view controller 渲染视图

exports.getShoppingCart = (req,res,next) => {

  if(!req.session.cart) {
    return  res.render("shop/cart", {
       path: "/cart",
       cakes: null
      });
  }
  let cart = new Cart(req.session.cart);
    res.render("shop/cart", {
     path: "/cart",
     cakes: cart.generateArray(), 
     totalPrice: cart.totalPrice
   });


}

I understand that this is happening due to the id.我知道这是由于 id 而发生的。 But is there a way to check for the cake size as well?但是有没有办法检查蛋糕的大小呢?

As advised by @Sergio in a comment above, On creating the shopping cart id, I added the cakesize to the end of the id: cakeId + size .正如@Sergio 在上面的评论中所建议的那样,在创建购物车 id 时,我将 cakesize 添加到 id 的末尾: cakeId + size That did the trick那成功了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将不同尺寸的相同产品添加到购物车 - Add same product with different sizes to the cart 如何防止在购物车中两次添加相同的产品 - how to prevent adding the same product 2 times in the shopping cart 如何将相同的商品添加到购物车 - How to Add same item to cart 如何将产品项目添加到购物车以及如何在始终(重新)计算购物车项目的总价的同时将其从那里删除? - How does one add a product-item to a shopping-cart and how would one remove it from there while always (re)calculating the cart items' total price? 添加到购物车后如何留在同一产品上? - How to stay on Same Product after Add to Cart? 如何找到在 asp:repeater 中选择的产品,因为我想将此选定的产品添加到购物车中 - how find which product is selected in asp:repeater because i want to add this selected product in shopping cart 如何增加/减少购物车中产品的数量? - How to increase / decrease the quantity of a product in a shopping cart? 如何计算购物车产品运费 - How to calculate shopping cart product shipping 具有本地存储功能的购物车-如何删除产品 - Shopping Cart with localstorage - how to remove product 有没有一种方法可以将多个相同的对象添加到购物车? - Is there a way to add multiple of the same object to a shopping cart?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM