/// <summary> /// Adds item to cart when user clicks button /// </summary> /// <returns></returns> public IActionResult AddItemToCart(InventoryProductInfo selectedItem, int?numOrdered) { if (numOrdered == null) { numOrdered = (int)1; } else { numOrdered = (int)numOrdered; } //TODO Add a check to make sure quantity isnt more than whats available //Not returning anything, just altering the cart list //Cart is a list of OrderInformation because that model contains all the relevant info on the ordered Product var loggedIn = _cache.Get("loggedInCustomer"); loggedInCustomer = (Customers)loggedIn; var itemCart = _cache.Get("Cart"); List <OrderInformation> cart = new List <OrderInformation>(); cart = (List <OrderInformation>)itemCart; //Check to see if the product is already in the cart or a new cart item OrderInformation newSKU = new OrderInformation(); newSKU.OrderedProduct = selectedItem.Skunum; newSKU.OrderedProductAmount = selectedItem.OrderedAmt; int matchWasFound = 0; foreach (var item in cart) { if (newSKU.OrderedProduct == item.OrderedProduct) { matchWasFound++; } } if (matchWasFound != 0) { //Grab the item from the cart var existingItem = StoreMethods.SelectCartItem(newSKU, cart); //Update the existing item's ordered amount and the total price for the item existingItem.OrderedProductAmount += newSKU.OrderedProductAmount; existingItem.UnitPrice = existingItem.OrderedProductAmount * (existingItem.UnitPrice * (1 - existingItem.ProductDiscount)); _cache.Set("Cart", cart); return(RedirectToAction("GetCustomerStoreInventory")); } else //If this is the first time this item has been added, create a new cart object { OrderInformation newCartItem = new OrderInformation(); newCartItem.OrderedProduct = selectedItem.Skunum; newCartItem.OrderedProductAmount = (int)numOrdered; newCartItem.ProductName = selectedItem.ProductName; newCartItem.UnitPrice = (int)numOrdered * (selectedItem.UnitPrice * (1 - selectedItem.ProductDiscount)); //Total price of that item given the discount and num ordered newCartItem.WhoOrdered = loggedInCustomer.CustomerId; newCartItem.StoreOrderedFrom = loggedInCustomer.DefaultStore; newCartItem.IsBundle = selectedItem.IsBundle; newCartItem.IsInBundle = selectedItem.IsInBundle; newCartItem.BundleId = selectedItem.BundleId; cart.Add(newCartItem); //Add new cart item to our cart _cache.Set("Cart", cart); //Set our cart to be the new cart return(RedirectToAction("GetCustomerStoreInventory")); } }