Beispiel #1
0
        public async Task UpdateCartProductAsync(CartProductsViewModel model)
        {
            var totalPrice = 0m;

            var currentProduct = await this.cartProductRepository
                                 .AllAsNoTracking()
                                 .Where(x => x.Id == model.Id)
                                 .FirstOrDefaultAsync();

            currentProduct.Quantity      = model.Quantity;
            currentProduct.SubTotalPrice = model.Price * currentProduct.Quantity;

            this.cartProductRepository.Update(currentProduct);
            await this.cartProductRepository.SaveChangesAsync();

            var currentCart = await this.cartRepository
                              .All()
                              .Where(x => x.Id == currentProduct.CartId)
                              .Include(c => c.CartProducts)
                              .FirstOrDefaultAsync();

            foreach (var product in currentCart.CartProducts)
            {
                totalPrice += product.SubTotalPrice;
            }

            currentCart.TotalPrice = totalPrice;

            this.cartRepository.Update(currentCart);
            await this.cartRepository.SaveChangesAsync();
        }
Beispiel #2
0
        public ActionResult CartProducts(string productIDs)
        {
            CartProductsViewModel model = new CartProductsViewModel();

            if (!string.IsNullOrEmpty(productIDs))
            {
                model.ProductIDs = productIDs.Split('-').Select(x => int.Parse(x)).Where(x => x > 0).ToList();

                if (model.ProductIDs.Count > 0)
                {
                    model.Products = ProductsService.Instance.GetProductsByIDs(model.ProductIDs.Distinct().ToList());
                }
            }

            return(PartialView("_CartProducts", model));
        }
        public async Task <IActionResult> UpdateCartProduct([FromBody] CartProductsViewModel currentProduct)
        {
            await this.cartService.UpdateCartProductAsync(currentProduct);

            return(this.Json(currentProduct));
        }
Beispiel #4
0
        public IActionResult Index(int Id)
        {
            var allProducts   = _product.GetProducts();
            var singleProduct = _product.GetProductById(Id);

            var model = new IndexViewModel();

            if (singleProduct != null)
            {
                // add new product to cart
                var productToCart = new Cart
                {
                    ProdId    = singleProduct.ProdId,
                    ProdName  = singleProduct.ProductName,
                    UnitPrice = singleProduct.Price,
                    AddedBy   = 1,
                };

                //**********reduce the card item quantity*************//
                if ((singleProduct.Quantity - 1) >= 0)
                {
                    singleProduct.Quantity -= 1;
                    _product.UpdateProduct(singleProduct);

                    _cart.AddToCart(productToCart);
                }
                //******************************************************//


                // get items added by current user
                var result = _cart.GetItemsAddedByUser(1);

                // group items by product and get the quantity and total price
                var quantity = from r in result group r by r.ProdId into newGroup
                               select new { Id    = newGroup.Key, qty = newGroup.Count(),
                                            total = newGroup.Sum(x => x.UnitPrice), };

                var list = new List <CartProductsViewModel>();
                // construct each object with total quantity and total price
                foreach (var item in quantity)
                {
                    var cartModel = new CartProductsViewModel
                    {
                        Id          = model.CartProducts.Count() + 1,
                        ProductId   = item.Id,
                        ProductName = _product.GetProductById(item.Id).ProductName,
                        Quantity    = item.qty,
                        UnitPrice   = _product.GetProductById(item.Id).Price,
                        Price       = item.total
                    };
                    list.Add(cartModel);
                }

                // add to view model
                model.CardProducts = allProducts;
                model.CartProducts = list;
                model.GrandTotal   = quantity.Sum(x => x.total);
                return(View(model));
            }

            model.CardProducts = allProducts;
            model.CartProducts = null;
            model.GrandTotal   = 0;
            return(View(model));
        }