public void Add2Cart(int ProductID, int Price, string ProductTitle) { int cartID = 0; int count = 0; if (User.Identity.IsAuthenticated) { var model = new Cart(); //MembershipUser MemberUser = Membership.GetUser(User.Identity.Name); //string UserId = MemberUser.ProviderUserKey.ToString(); string UserId = Convert.ToString(Common.GetUserId(HttpContext.User.Identity.Name)); cartID = BLObj.GetCartIDForUser(Convert.ToInt32(UserId)); if (cartID == 0) { BLObj.InsertCartIDForUserID(Convert.ToInt32(UserId)); cartID = BLObj.GetCartIDForUser(Convert.ToInt32(UserId)); } count = BLObj.GetProductCountFromCurrentCartItems(Convert.ToInt32(UserId), cartID, ProductID); if (count == 0) { model.CartID = cartID; model.UserID = Convert.ToInt32(UserId); model.ProductID = ProductID; model.Quantity = 1; model.Price = Price; BLObj.CurrentCartItems_Insert(model); } else { model.CartID = cartID; model.UserID = Convert.ToInt32(UserId); model.ProductID = ProductID; model.Price = Price; BLObj.CurrentCartItems_Update(model); } } else { if (Cart.Current.Count == 0) { var model = Cart.Current; model.Add(new Cart { ProductID = ProductID, Price = Price, ItemPrice = Price, TotalAmt = Price, Quantity = 1, ProductTitle = ProductTitle, SRNO = 1 }); HttpContext.Session["Cart"] = model; } else { var model = Cart.Current; int flag = 0; for (int i = 0; i < model.Count; i++) { if (model[i].ProductID == ProductID) { model[i].Quantity = model[i].Quantity + 1; model[i].ItemPrice = model[i].ItemPrice + Price; model[0].TotalAmt = model[0].TotalAmt + Price; flag = 1; } } if (flag == 0) { int totalamt = model[0].TotalAmt; model.Add( new Cart { SRNO=model.Count + 1, ProductID = ProductID, Price = Price, ItemPrice = Price, Quantity = 1, ProductTitle = ProductTitle } ); model[0].TotalAmt = model[0].TotalAmt + Price; } HttpContext.Session["Cart"] = model; } } }
public ActionResult ShoppingCart() { var model = new Cart(); if (User.Identity.IsAuthenticated) { int CartID = 0; //MembershipUser MemberUser = Membership.GetUser(User.Identity.Name); //string UserId = MemberUser.ProviderUserKey.ToString(); string UserId = Convert.ToString(Common.GetUserId(HttpContext.User.Identity.Name)); CartID = BLObj.GetCartIDForUser(Convert.ToInt32(UserId)); model.ListData = BLObj.CurrentCartItems_Get(Convert.ToInt32(UserId), CartID); if (model.ListData.Count == 0) { model.TotalAmt = 0; } else { model.TotalAmt = model.ListData[0].TotalAmt; } if (Request.IsAjaxRequest()) { return PartialView("_WebgridShoppingCart", model); } } else { model.ListData=Cart.Current; if (model.ListData.Count > 0) { model.TotalAmt = model.ListData[0].TotalAmt; } else { model.TotalAmt = 0; } if (Request.IsAjaxRequest()) { return PartialView("_WebgridShoppingCart", model); } } return View(model); }