public void ThemSanPham(SanPham sp, int sl, bool laCapNhat) { var SanPhamGioHang = new SanPhamGioHang() { MaSP = sp.MaSP, TenSP = sp.TenSP, MieuTa = sp.MieuTa, GiaSP = sp.GiaSP, AnhSP = sp.AnhSP, XuatSu = sp.XuatSu, ChatLieu = sp.ChatLieu, GioiTinh = sp.GioiTinh, MaLoaiSanPham = sp.MaLoaiSanPham, LoaiSanPham = sp.LoaiSanPham.TenLoaiSanPham, SoLuong = sl, }; // kiểm tra tồn tại Product có trong giỏ hàng theo id chưa var existKey = SanPhams.ContainsKey(sp.MaSP); if (!laCapNhat && existKey) { var existingItem = SanPhams[sp.MaSP]; SanPhamGioHang.SoLuong += existingItem.SoLuong; } if (existKey) { SanPhams[sp.MaSP] = SanPhamGioHang; } else { SanPhams.Add(sp.MaSP, SanPhamGioHang); } }
public async Task <IActionResult> ThemGioHang(int idSP, int soLuong) { var sanPham = await _context.SanPham .SingleOrDefaultAsync(m => m.ID == idSP); var cart = HttpContext.Session.GetSession <List <SanPhamGioHang> >(GioHangSession); if (cart == null) { cart = HttpContext.Request.Cookies.GetKookies <List <SanPhamGioHang> >(GioHangSession); } if (cart != null) { var list = (List <SanPhamGioHang>)cart; if (list.Exists(x => x.idSP == idSP)) { foreach (var item in list) { if (item.idSP == idSP) { item.SoLuong += soLuong; int?GiaThem = soLuong * item.Gia; item.TongTien = item.TongTien + GiaThem; } } } else { var item = new SanPhamGioHang(); item.idSP = sanPham.ID; item.TenSP = sanPham.TenSPCoDau; item.Gia = sanPham.GiaGiam; item.HinhAnh = sanPham.HinhAnh; item.SoLuong = soLuong; item.TongTien = soLuong * sanPham.GiaGiam; list.Add(item); } //Gan vao session HttpContext.Session.SetSession(GioHangSession, list); HttpContext.Response.Cookies.SetKookies(GioHangSession, list); } else { var item = new SanPhamGioHang(); item.idSP = sanPham.ID; item.TenSP = sanPham.TenSPCoDau; item.Gia = sanPham.GiaGiam; item.HinhAnh = sanPham.HinhAnh; item.SoLuong = soLuong; item.TongTien = soLuong * sanPham.GiaGiam; var list = new List <SanPhamGioHang>(); list.Add(item); //Gan vao session HttpContext.Session.SetSession(GioHangSession, list); HttpContext.Response.Cookies.SetKookies(GioHangSession, list); } return(Content("The vao gio hang thanh cong", "text/plain")); }
public ActionResult ThemGioHang(string idsp) { sanpham objSp = db.sanphams.Find(idsp); if (Session["GioHang"] == null) { SanPhamGioHang spgh = new SanPhamGioHang(); int value; spgh.id = objSp.id; spgh.gia = 0; spgh.urlhinh = objSp.hinhdaidien; try { spgh.gia = Convert.ToInt32(objSp.giaban); } catch { } spgh.soluong = 1; spgh.ten = objSp.ten; List <SanPhamGioHang> listSPGH = new List <SanPhamGioHang>(); listSPGH.Add(spgh); Session["GioHang"] = listSPGH; } else { //Kiem tra hang vua them da ton tai chua (de cap nhat so luong), neu chua, them mon do vao gio hang List <SanPhamGioHang> listSPGH = Session["GioHang"] as List <SanPhamGioHang>; SanPhamGioHang spthem = listSPGH.FirstOrDefault(s => s.id == idsp); if (spthem == null) { SanPhamGioHang spgh = new SanPhamGioHang(); int value; spgh.id = objSp.id; spgh.gia = 0; spgh.urlhinh = objSp.hinhdaidien; try { spgh.gia = Convert.ToInt32(objSp.giaban); } catch { } spgh.soluong = 1; spgh.ten = objSp.ten; listSPGH.Add(spgh); } else { spthem.soluong++; } Session["GioHang"] = listSPGH; } return(RedirectToAction("Purchase", "Cart")); }
public ActionResult TangSoLuong(string idsp_tang) { List <SanPhamGioHang> listSPGH = Session["GioHang"] as List <SanPhamGioHang>;; if (listSPGH != null) { SanPhamGioHang spgiohang = listSPGH.FirstOrDefault(s => s.id == idsp_tang); if (spgiohang != null) { spgiohang.soluong++; } Session["GioHang"] = listSPGH; } return(RedirectToAction("Purchase", "Cart")); }
public ActionResult Product(int masp, int soLuong) { FSanPham fSanPham = new FSanPham(); FSanPhamGioHang fSanPhamGioHang = new FSanPhamGioHang(); FGioHang fGioHang = new FGioHang(); var sp = fSanPham.FindSanPham(masp); if (sp.TenSP.Length > 40) { sp.TenSP = sp.TenSP.Substring(0, 35) + "..."; } var gioHang = (GioHang)Session["GioHang"]; SanPhamGioHang sanPhamGioHang = new SanPhamGioHang(); if (Session["KhachHang"] != null) { KhachHang x = (KhachHang)Session["KhachHang"]; if (gioHang == null) { gioHang = fGioHang.NewGH(x); } gioHang = fSanPhamGioHang.AddItem(masp, gioHang.ID, soLuong); Session["GioHang"] = gioHang; return(RedirectToAction("Shopcart")); } else { if (Request.Cookies["IDCart"] != null) { string x = Request.Cookies["IDCart"].Value; var giohangtam = fGioHang.GetGH_MaGH(Convert.ToInt32(x)); giohangtam = fSanPhamGioHang.AddItem(masp, Convert.ToInt32(x), soLuong); Session["GioHang"] = giohangtam; return(RedirectToAction("Shopcart")); } else { gioHang = fGioHang.NewGH(); Response.Cookies["IDCart"].Value = gioHang.ID.ToString(); Response.Cookies["IDCart"].Expires = DateTime.Now.AddDays(1); gioHang = fSanPhamGioHang.AddItem(masp, gioHang.ID, soLuong); Session["GioHang"] = gioHang; return(RedirectToAction("Shopcart")); } } }
public ActionResult XoaSanPhamGioHang(string idsp) { List <SanPhamGioHang> listSPGH = Session["GioHang"] as List <SanPhamGioHang>;; if (listSPGH != null) { SanPhamGioHang spgiohang = listSPGH.FirstOrDefault(s => s.id == idsp); if (spgiohang != null) { if (spgiohang.soluong > 1) { spgiohang.soluong--; } else { listSPGH.Remove(spgiohang); } } Session["GioHang"] = listSPGH; } return(RedirectToAction("Purchase", "Cart")); }