/// <summary> /// Add an item to the cart. /// When ItemId to be added has already existed, this method will update the quantity instead. /// </summary> /// <param name="item">Item to add</param> public void Add(CartItemInfo item) { CartItemInfo cartItem; if (!cartItems.TryGetValue(item.ItemId, out cartItem)) cartItems.Add(item.ItemId, item); else cartItem.Quantity += item.Quantity; }
/// <summary> /// Add an item to the cart. /// When ItemId to be added has already existed, this method will update the quantity instead. /// </summary> /// <param name="itemId">Item Id of item to add</param> public void Add(string itemId) { CartItemInfo cartItem; if (!cartItems.TryGetValue(itemId, out cartItem)) { Item item = new Item(); ItemInfo data = item.GetItem(itemId); if (data != null) { CartItemInfo newItem = new CartItemInfo(itemId, data.ProductName, 1, (decimal)data.Price, data.Name, data.CategoryId, data.ProductId); cartItems.Add(itemId, newItem); } } else cartItem.Quantity++; }