public ActionResult AddToCart(Models.CartItem item) { ShoppingCart myCart = new ShoppingCart(); if (SessionFacade.MyCart == null) SessionFacade.MyCart = myCart; else myCart = SessionFacade.MyCart; CartItem row = item; CartItem testrow = new CartItem(); // if item exists in shopping cart, remove first, then add // with an updated total count int nTotalItem = myCart.list.Count; for (int nItem = 0; nItem < nTotalItem; nItem++) { testrow = (CartItem)myCart.list[nItem]; if (testrow.ProductId == item.ProductId) { int qty = testrow.Quantity; qty += row.Quantity; testrow.Quantity = qty; testrow.TotalAmount = testrow.Quantity * testrow.Price; myCart.list.RemoveAt(nItem); row = testrow; break; } } myCart.list.Add(row); myCart.TotalQty = nTotalItem; TempData["msg"] = row.Quantity + " items of " + row.ProductSDesc + " added to your Shopping Cart"; return RedirectToAction("Detail", new { Id = item.ProductId }); }
// needed in implementing shopping // "Cancel Last Change" functionality. public object Clone() { ShoppingCart clone = new ShoppingCart(); clone.list = (ArrayList)this.list.Clone(); return clone; }
public ActionResult ViewCart() { ShoppingCart myCart = new ShoppingCart(); if (SessionFacade.MyCart == null) SessionFacade.MyCart = myCart; else myCart = SessionFacade.MyCart; myCart.items.Clear(); int nTotalItem = myCart.list.Count; myCart.TotalQty = nTotalItem; double Total = 0; for(int nItem =0; nItem < nTotalItem; nItem++) { CartItem Item = (CartItem)myCart.list[nItem]; Item.TotalAmount = Item.Quantity * Item.Price; myCart.items.Add(Item); Total += Item.TotalAmount; } myCart.TotalCost = Total; return View(myCart); }