public ActionResult ChangeQuantity(int id, int qty) { var cart = ShoppingCart.GetCart(this.HttpContext); // Get the name of the item to display confirmation int itemId = webStoreDb.Carts .Single(item => item.RecordId == id).ItemId; string itemName = mainDb.Items.Single(i => i.ItemID == itemId).Name; decimal itemPrice = mainDb.Items.Single(p => p.ItemID == itemId).Price; // Find out if new quantity is available string cartId = cart.GetCartId(this.HttpContext); var cartItem = webStoreDb.Carts.Single(c => c.CartId == cartId && c.RecordId == id); int inventoryID = cartItem.InventoryId; Inventory inventory = mainDb.Inventories.Single(i => i.InventoryID == inventoryID); int itemAvailableQty = inventory.QuantityInStock; var results = new ShoppingCartUpdateViewModel(); if (itemAvailableQty >= qty) { results.Success = true; cart.ChangeCartQty(id, qty); results.Message = itemName + " has been changed to " + qty + " unit"; if (qty == 1) results.Message += "."; else results.Message += "s."; results.CartTotal = cart.GetTotal(); results.CartCount = cart.GetCount(); results.ItemCount = qty; results.ItemTotal = itemPrice * qty; results.UpdateId = id; } else { results.Success = false; results.Message = "There are only " + itemAvailableQty + " of " + Server.HtmlEncode(itemName) + " available in that size."; } return Json(results); }
public ActionResult RemoveFromCart(int id) { var cart = ShoppingCart.GetCart(this.HttpContext); // Get the name of the item to display confirmation int itemId = webStoreDb.Carts .Single(item => item.RecordId == id).ItemId; string itemName = mainDb.Items.Single(i => i.ItemID == itemId).Name; decimal itemPrice = mainDb.Items.Single(p => p.ItemID == itemId).Price; // Remove from cart cart.RemoveFromCart(id); // Display the confirmation message var results = new ShoppingCartUpdateViewModel { Success = true, Message = itemName + " has been removed from your shopping cart.", CartTotal = cart.GetTotal(), CartCount = cart.GetCount(), UpdateId = id }; return Json(results); }