public ActionResult UpdateCartItemQuantity(int productId, int quantity) { try { var userEmail = CookieManager.GetEmailFromUserCookie(); var cartItem = services.CartService.GetCartItem(userEmail, productId); //update quantity of of cart item to quantity parameter value cartItem.Quantity = quantity; services.CartService.UpdateCartItem(cartItem); //Get cart items of user with cart item quantity updated var userCartItems = services.CartService.GetUserCartItems(userEmail, Constants.DATABASE_TABLE_PRODUCTS); var cartItemsCount = userCartItems.Sum(ci => ci.Quantity); var subTotal = userCartItems.Sum(ci => ci.Product.Price * ci.Quantity); //create viewmodel for associated view and pass into view UpdateCartItemQuantityViewModel updateCartItemQuantityViewModel = new UpdateCartItemQuantityViewModel(); updateCartItemQuantityViewModel.CartItemsCount = cartItemsCount.ToString(); updateCartItemQuantityViewModel.SubTotal = string.Format("{0:C}", subTotal); updateCartItemQuantityViewModel.SubTotalLabel = string.Format("({0} {1})", cartItemsCount, cartItemsCount == 1 ? "item": "items"); updateCartItemQuantityViewModel.CartLabel = string.Format("Cart ({0})", cartItemsCount); return(Json(updateCartItemQuantityViewModel)); } catch (Exception ex) { ExceptionManager.LogException(ex, Path.GetFileName(Request.PhysicalPath)); return(RedirectToAction(Constants.CONTROLLER_ACTION_INDEX, Constants.CONTROLLER_ERROR)); } }
public ActionResult UpdateCartItemQuantity(UpdateCartItemQuantityViewModel model) { var importantCartUpdates = new List <String>(); if (ModelState.IsValid) { var existingItem = _cartService.GetItem(model.CartItemID); if (existingItem == null) { //return HttpNotFound("The cart item to update could not be found."); throw new HttpException(500, "The cart item to update could not be found."); } //return new HttpStatusCodeResult(System.Net.HttpStatusCode.NotFound, "The cart item to update could not be found."); if (existingItem.ShoppingCartID != this._shoppingCartID) { throw new HttpException(403, "The cart item does not belong to the current user."); } //return new HttpStatusCodeResult(System.Net.HttpStatusCode.Unauthorized, "The cart item does not belong to the current user."); if (model.Quantity <= 0) { _cartService.Delete(existingItem); importantCartUpdates.Add("The item has been successfully removed from your cart."); } else { var product = _productService.Get(existingItem.ProductID); if (model.Quantity >= product.UnitsInStock) { existingItem.Quantity = product.UnitsInStock; _cartService.Update(existingItem); importantCartUpdates .Add(String.Format("You requested more than the available amount for {0}. " + "The item has been updated to reflect the available quantity of ({1}).", product.ProductName, product.UnitsInStock)); } else { existingItem.Quantity = model.Quantity; _cartService.Update(existingItem); importantCartUpdates .Add("The item has been successfully updated."); } } _uow.Save(); } else { TempData.Add("ModelState", this.allErrorsToModelErrors(this.ModelState)); } TempData.Add("ImportantCartUpdates", importantCartUpdates); return(RedirectToAction("Index")); #region Ajax ////if (Request.IsAjaxRequest()) ////{ //// var jsonResult = new JsonDataResultViewModel(); //// jsonResult.Success = true; //// return Json(jsonResult); ////} #endregion }