Beispiel #1
0
        public ActionResult RemoveFromCart(int id, bool removeAll)
        {
            var cart = ShoppingCart.GetCart(this.HttpContext);

            string productName = context.Carts
                                 .Single(item => item.ProductId == id).Product.productName;

            int itemCount;

            if (removeAll)
            {
                itemCount = cart.RemoveFromCart(id, removeAll);
            }
            else
            {
                itemCount = cart.RemoveFromCart(id, removeAll);
            }

            var results = new VM_ShoppingCartUpdate()
            {
                Message = Server.HtmlEncode(productName) +
                          " has been removed from your shopping cart.",
                CartTotal = cart.GetTotal(),
                CartCount = cart.GetCount(),
                ItemCount = itemCount,
                ProductId = id
            };

            Response.AddHeader("Refresh", "5");

            return(Json(results));
        }
Beispiel #2
0
        public ActionResult UpdateCart(int id, int updateQty)
        {
            // Remove Whole Product from Shopping Cart if User enters zero or less
            if (updateQty <= 0)
            {
                return(RemoveFromCart(id, true));
            }

            else
            {
                var addedProduct = context.Products
                                   .Single(product => product.productId == id);

                var cart = ShoppingCart.GetCart(this.HttpContext);

                string productName = context.Carts
                                     .Single(item => item.ProductId == id).Product.productName;

                // Update Quantity equals to total quantity in stock
                //  if user enters higher amount
                if (updateQty > addedProduct.quantity)
                {
                    updateQty = addedProduct.quantity;
                }

                bool updated = cart.UpdateCart(id, updateQty);

                string updateMessage = (updated == true)
                    ? Server.HtmlEncode(productName) +
                                       " quantity has been updated"
                    : "";

                ViewData["Message"] = updateMessage;
                //ViewData["CartTotal"] = cart.GetTotal();
                //ViewData["CartCount"] = cart.GetCount();
                //ViewData["ItemCount"] = updateQty;
                //ViewData["ProductId"] = id;

                var results = new VM_ShoppingCartUpdate
                {
                    Message   = updateMessage,
                    CartTotal = cart.GetTotal(),
                    CartCount = cart.GetCount(),
                    ItemCount = updateQty,
                    ProductId = id
                };

                //return View(Json(results));
                return(Json(results));
            }
        }