public void AddToCart(Product product) { // Get the matching cart and album instances var cartItem = storeDB.Carts.SingleOrDefault( p => p.CartID == ShopCartID && p.ProductID == product.ProductID); if (cartItem == null) { // Create a new cart item if no cart item exists cartItem = new Cart { ProductID = product.ProductID, CartID = ShopCartID, Count = 1 }; storeDB.Carts.Add(cartItem); } else { // If the item does exist in the cart, then add one to the quantity cartItem.Count++; } // Save changes storeDB.SaveChanges(); }
public void AddToCart(Product product) { // Get the matching cart and album instances var cart = GetCart(cartSessionID); var cartItem = cart.CartItems.SingleOrDefault(i => i.ProductID == product.ProductID); Debug.WriteLine("Add to cart: " + cartSessionID); if (cartItem == null) { // Create a new cart item if no cart item exists cartItem = new CartItem { ProductID = product.ProductID, CartID = cart.ID, Quantity = 1, }; storeDB.CartItems.Add(cartItem); } else { // If the item does exist in the cart, // then add one to the quantity cartItem.Quantity += 1; Debug.WriteLine(cartItem.Quantity); } // Save changes Debug.WriteLine("Before : " + cartItem.Quantity); storeDB.SaveChanges(); Debug.WriteLine("After " + cartItem.Quantity); }
private Cart CreateCartIfItDoesntExist(string sessionID, Cart cart) { if (cart == null) { cart = new Cart { SessionID = sessionID, CartItems = new List <CartItem>() }; storeDB.Carts.Add(cart); storeDB.SaveChanges(); } return(cart); }