Beispiel #1
0
        /// <summary>
        /// 新增商品至購物車:
        /// 先檢查此商品是否已存在購物車,
        /// 若是則數量加1,
        /// 若否則新增product至購物車內。
        /// </summary>
        /// <param name="Id">商品編號(Product Id)</param>
        public void AddProduct(int?Id)
        {
            var tmpItem = this.cartItems
                          .Where(items => items.Id == Id)
                          .Select(items => items)
                          .FirstOrDefault();

            if (tmpItem == default(CartItem))
            {
                ProductEntity pe     = new ProductEntity();
                CartItem      dbitem = pe.QueryByProductid(Id);

                if (dbitem != default(CartItem))
                {
                    this.AddCarItem(dbitem);
                }

                ////若在購物車,沒有同樣的商品
                //using (WelcomeBooksEntities db = new WelcomeBooksEntities())
                //{
                //    var product = (from prod in db.Products
                //                   where prod.Id == Id
                //                   select prod).FirstOrDefault();


                //    if (product != default(Products))
                //    {
                //        this.AddProduct(product);
                //    }

                //}
            }
            else
            {
                //若在購物車,已有同樣的商品,則數量加1
                tmpItem.Quantity += 1;
            }
        }