//新增一筆Product,使用商品編號
        public bool AddProduct(int ProductId)
        {
            CartItem findItem = this.cartItems
                                .Where(s => s.Id == ProductId)
                                .Select(s => s)
                                .FirstOrDefault();

            //判斷商品是否已在購物車
            if (findItem == default(CartItem)) //商品不存在,新增一筆
            {
                //取得商品
                Product product = new Product();
                using (CartsEntities db = new CartsEntities())
                {
                    product = (from s in db.Products where s.Id == ProductId select s).FirstOrDefault();
                }

                //如果有這個商品
                if (product != default(Product))
                {
                    this.AddProduct(product);
                }
            }
            else //商品已存在,數量+1
            {
                findItem.Quantity += 1;
            }

            return(true);
        }
Exemple #2
0
        //新增一筆Product,使用ProductId
        public bool AddProduct(int ProductId)
        {
            var findItem = cartItems.Where(p => p.Id == ProductId).FirstOrDefault();

            //判斷相同Id的CartItem是否已經存在購物車內
            if (findItem == null)
            {
                using (CartsEntities db = new CartsEntities())
                {
                    var product = db.Product.Where(p => p.Id == ProductId).FirstOrDefault();
                    if (product != null)
                    {
                        this.AddProduct(product);
                    }
                }
            }
            else
            {
                //存在購物車內,則將商品數量增加
                findItem.Quantity += 1;
            }
            return(true);
        }