示例#1
0
        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);
            }
        }
示例#2
0
 public void Remove(int MaSP)
 {
     if (SanPhams.ContainsKey(MaSP))
     {
         SanPhams.Remove(MaSP);
     }
 }