protected string AddCart(Cart cart) { try { var cartExist = Session["cart"] != null; if (cartExist && IsInCart(cart) == false) { List<Cart> carts = (List<Cart>)Session["cart"]; carts.Add(cart); Session["cart"] = carts; } else if (cartExist && IsInCart(cart)) { List<Cart> carts = (List<Cart>)Session["cart"]; foreach (Cart p in carts) { if (p.ProductID == cart.ProductID) { p.Amount += cart.Amount; Session["cart"] = carts; break; } } } else { Session["cart"] = new List<Cart> { cart }; } return "Order was succesfully added"; } catch (Exception e) { return "Error:" + e; } }
private static Models.Cart RemoveReference(this Models.Cart item) { var obj = new Models.Cart(); if (item != null) { obj.Id = item.Id; obj.IsActive = item.IsActive; obj.AdditionalCost = item.AdditionalCost; obj.IpAddress = item.IpAddress; obj.IsConvertToCheckout = item.IsConvertToCheckout; obj.OrderDate = item.OrderDate; obj.OrderNumber = item.OrderNumber; //obj.TotalAmount = item.TotalAmount; obj.UserId = item.UserId; obj.VendorId = item.VendorId; } return(obj); }
protected void btnAdd_Click(object sender, EventArgs e) { if (!string.IsNullOrWhiteSpace(Request.QueryString["id"])) { string clientId = "-1"; int id = Convert.ToInt32(Request.QueryString["id"]); int amount = Convert.ToInt32(ddlAmount.SelectedValue); Cart cart = new Cart() { Amount = amount, ClientID = clientId, DatePurchased = DateTime.Now, IsInChart = true, ProductID = id }; lblResult.Text = AddCart(cart); Response.RedirectPermanent("~/Pages/Index.aspx"); } }
public ActionResult AddCartItem(int bookId) { //string bookId = Request.QueryString["bookId"]; //得到当前用户 Customer customer = Session["user"] as Customer; //判断当前用户是否有购物车 var cart = bse.Carts.Where(c => c.customerId == customer.id).FirstOrDefault(); if (cart == null) { //没有则创建购物车 customer = bse.Customers.Where(c => c.id == customer.id).FirstOrDefault(); cart = new Models.Cart(customer); bse.Carts.Add(cart); bse.SaveChanges(); } var cartItem = cart.CartItems.Where(c => c.bookId == bookId).FirstOrDefault(); if (cartItem == null) { var book = bse.Books.Where(b => b.id == bookId).FirstOrDefault(); cartItem = new CartItem(book); cart.CartItems.Add(cartItem); } //更新购物车明细的数量和价钱 cartItem.num += 1; cartItem.price = cartItem.Book.price * cartItem.num; //跟新购物车的明细和价钱 cart.num += 1; cart.price += cartItem.Book.price; bse.SaveChanges(); return(RedirectToAction("Cart", "Home")); }
public ActionResult AddCartItem(int bookId) { //string bookId = Request.QueryString["bookId"]; //得到当前用户 Customer customer = Session["user"] as Customer; //判断当前用户是否有购物车 var cart = bse.Carts.Where(c => c.customerId == customer.id).FirstOrDefault(); if (cart == null) { //没有则创建购物车 customer = bse.Customers.Where(c => c.id == customer.id).FirstOrDefault(); cart = new Models.Cart(customer); bse.Carts.Add(cart); bse.SaveChanges(); } var cartItem = cart.CartItems.Where(c => c.bookId == bookId).FirstOrDefault(); if (cartItem == null) { var book = bse.Books.Where(b => b.id == bookId).FirstOrDefault(); cartItem = new CartItem(book); cart.CartItems.Add(cartItem); } //更新购物车明细的数量和价钱 cartItem.num += 1; cartItem.price = cartItem.Book.price * cartItem.num; //跟新购物车的明细和价钱 cart.num += 1; cart.price += cartItem.Book.price; bse.SaveChanges(); return RedirectToAction("Cart", "Home"); }
private bool IsInCart(Cart cart) { List<Cart> carts = (List<Cart>) Session["cart"]; foreach (Cart c in carts) { if (c.ProductID == cart.ProductID) return true; } return false; }