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 void Remove(int MaSP) { if (SanPhams.ContainsKey(MaSP)) { SanPhams.Remove(MaSP); } }