// AJAX: /ShoppingCart/AddItemToShoppingCart/ public JsonResult AddItemToShoppingCart(ShoppingCartItemViewModel itemModel) { // Check if there's a cart for this session ShoppingCart shoppingCart = _shoppingCartService.GetShoppingCart(CookieFactory.GetShoppingCartCookie(Request, Response)); ShoppingCartItem item = Mapper.Map <ShoppingCartItemViewModel, ShoppingCartItem>(itemModel); ShoppingCartItem retItem = _shoppingCartItemService.AddOrUpdateShoppingCartItem(item, shoppingCart.Guid); _shoppingCartItemService.SaveShoppingCartItem(); ShoppingCartItemViewModel newModel = Mapper.Map <ShoppingCartItem, ShoppingCartItemViewModel>(retItem); decimal total = _offerService.GetTotal(shoppingCart, _itemService.GetItems()); return(Json(new { newModel, total = total }, JsonRequestBehavior.AllowGet)); }
// AJAX: /ShoppingCart/RemoveFromShoppingCart/ public JsonResult RemoveFromShoppingCart(string name) { // Check if there's a cart for this session ShoppingCart shoppingCart = _shoppingCartService.GetShoppingCart(CookieFactory.GetShoppingCartCookie(Request, Response)); if (shoppingCart != null) { bool success = _shoppingCartItemService.DeleteShoppingCartItemByName(name); _shoppingCartService.SaveShoppingCart(); decimal total = _offerService.GetTotal(shoppingCart, _itemService.GetItems()); return(Json(new { success = success, total = total }, JsonRequestBehavior.AllowGet)); } return(Json(null, JsonRequestBehavior.AllowGet)); }
public ActionResult Index() { IEnumerable <Item> items = _itemService.GetItems().ToList(); IEnumerable <ItemViewModel> modelItems = Mapper.Map <IEnumerable <Item>, IEnumerable <ItemViewModel> >(items); // Check if there's a cart for this session ShoppingCart shoppingCart = _shoppingCartService.GetShoppingCart(CookieFactory.GetShoppingCartCookie(Request, Response)); _shoppingCartService.SaveShoppingCart(); ShoppingCartViewModel cartModel = Mapper.Map <ShoppingCart, ShoppingCartViewModel>(shoppingCart); return(View(new ItemsAndCartViewModel { Items = modelItems, ShoppingCart = cartModel, Total = _offerService.GetTotal(shoppingCart, items) })); }