技术开发 频道

利用对象序列化将购物车保存在Cookie中


【IT168技术文档】

  购物车类:ShopCart.cs(说明:主要利用hashtable保存商品对象)
using System; using System.Collections; /// <summary> /// 购物车类 /// </summary> [Serializable] public class ShopCart { public Hashtable _CartItems = new Hashtable(); /// <summary> /// 构造函数 /// </summary> public ShopCart() { ///to do something } /// <summary> /// 返回购物车中的所有商品(接口类型) /// </summary> public ICollection CartItems { get { return _CartItems.Values; } } /// <summary> /// 购物中所有商品的价格合计 /// </summary> public double Total { get { double sum = 0; foreach (ShopCartItem item in _CartItems.Values) { sum += ((item.Price * item.Quantity) + item.SendPrice); } return sum; } } /// <summary> /// 返回购物车里所有商品的数量 /// </summary> public double TotalNum { get { double sum = 0; foreach (ShopCartItem item in _CartItems.Values) { sum += item.Quantity; } return sum; } } /// <summary> /// 向购物车里添加某商品 /// </summary> /// <param name="ID">商品ID</param> /// <param name="Name">商品名称</param> /// <param name="Price">商品单价</param> /// <param name="AutoAddQuantity">如果购物车中已经存在该商品,该商品的数量是否加一,True数量加1,False数量不变</param> public void AddItem(string ID, string Name, string DeliveryName, double Price, int Score, string ProductId, string PicUrl, double MarketPrice, double UserPrice, double VipPrice, double SendPrice,string ShopID, string UrlFrom, bool AutoAddQuantity) { ShopCartItem item = (ShopCartItem)_CartItems[ID]; if(item == null) _CartItems.Add(ID, new ShopCartItem(ID, Name, DeliveryName, Price, Score, ProductId, PicUrl, MarketPrice, UserPrice, VipPrice, SendPrice,ShopID, UrlFrom)); else { if(AutoAddQuantity) { item.Quantity++; } _CartItems[ID] = item; } } /// <summary> /// 从购物车里移除某商品 /// </summary> /// <param name="ID">商品ID</param> /// <param name="FullDelete">如果商品数量大于1,是否彻底从购物车中删除该种商品,true彻底从购物车中删除该种商品,false则仅将该种商品数量减一</param> public void RemoveItem(string ID, bool FullDelete) { ShopCartItem item = (ShopCartItem)_CartItems[ID]; if(item == null) { return; } else { if(FullDelete) { _CartItems.Remove(ID); } else { item.Quantity--; if(item.Quantity == 0) { _CartItems.Remove(ID); } else { _CartItems[ID] = item; } } } } /// <summary> /// 修改购物车里某商品的数量 /// </summary> /// <param name="ID">商品ID</param> /// <param name="Quantity">商品数量</param> public void UpdateItem(string ID, int Quantity) { ShopCartItem item = (ShopCartItem)_CartItems[ID]; if(item == null) { return; } else { if(Quantity > 0) //商品数量必须大于0 { item.Quantity = Quantity; } _CartItems[ID] = item; } } /// <summary> /// 清空购物车 /// </summary> public void ClearCart() { _CartItems.Clear(); } } /// <summary> /// [购物车具体]商品类 /// </summary> [Serializable] public class ShopCartItem { private string _ID;//产品GUID private string _Name;//产品名称 private string _DeliveryName;//物流产品名 private double _Price=0;//产品单价(结算价格) private int _Score = 0;//产品单个积分 private int _Quantity = 1;//产品数量 private string _ProductId;//产品自定义编号 private string _PicUrl;//图片地址 private double _MarketPrice=0;//市场价格 private double _VipPrice=0;//VIp价格 private double _UserPrice=0;//会员价格 private double _SendPrice=0;//运输费用 private string _ShopID;//商家ID private string _UrlFrom; //页面来源 /// <summary> /// 属性:商品ID /// </summary> public string ID { get { return _ID; } } /// <summary> /// 属性:商品名称Name /// </summary> public string Name { get { return _Name; } } /// <summary> /// 属性:商品单价Price /// </summary> public double Price { get { return _Price; } } /// <summary> /// 单件产品所获积分 /// </summary> public int Score { get { return _Score; } set { _Score = value; } } /// <summary> /// 产品编号 /// </summary> public string ProductId { get { return _ProductId; } set { _ProductId = value; } } /// <summary> /// 产品图片地址 /// </summary> public string PicUrl { get { return _PicUrl; } set { _PicUrl = value; } } /// <summary> /// 市场价格 /// </summary> public double MarketPrice { get { return _MarketPrice; } set { _MarketPrice = value; } } /// <summary> /// VIP价格 /// </summary> public double VipPrice { get { return _VipPrice; } set { _VipPrice = value; } } /// <summary> /// 会员价格 /// </summary> public double UserPrice { get { return _UserPrice; } set { _UserPrice = value; } } /// <summary> /// 运输费用 /// </summary> public double SendPrice { get { return _SendPrice; } set { _SendPrice = value; } } /// <summary> /// 属性:商品数量Quantity /// </summary> public int Quantity { get { return _Quantity; } set { _Quantity = value; } } /// <summary> /// 商家ID /// </summary> public string ShopID { get { return _ShopID; } set { _ShopID = value; } } /// <summary> /// 页面来源 /// </summary> public string UrlFrom { get { return _UrlFrom; } set { _UrlFrom = value; } } /// <summary> /// 购物车商品项完整构架函数 /// </summary> /// <param name="ID">产品ID</param> /// <param name="Name">产品名称</param> /// <param name="DeliveryName">产品物流名称</param> /// <param name="Price">产品单价(计算价)</param> /// <param name="Score">产品积分</param> /// <param name="ProductId">产品编号</param> /// <param name="PicUrl">产品图片</param> /// <param name="MarketPrice">会员价格</param> /// <param name="UserPrice">市场价格</param> /// <param name="VipPrice">VIp价格</param> /// <param name="SendPrice">运输费用</param> /// <param name="ShopID">商家ID</param> /// <param name="UrlFrom">页面来源</param> public ShopCartItem(string ID, string Name, string DeliveryName, double Price, int Score, string ProductId, string PicUrl, double MarketPrice, double UserPrice, double VipPrice, double SendPrice, string ShopID, string UrlFrom) { _ID = ID; _Name = Name; _DeliveryName = DeliveryName; _Quantity = 1; _Price = Price; _Score = Score; _ProductId = ProductId; _PicUrl = PicUrl; _MarketPrice = MarketPrice; _UserPrice = UserPrice; _VipPrice = VipPrice; _SendPrice = SendPrice; _ShopID = ShopID; _UrlFrom = UrlFrom; } /// <summary> /// 购物车商品项简单构架函数 /// </summary> /// <param name="ID">产品ID</param> /// <param name="Name">产品名称</param> /// <param name="Price">产品单价(计算价)</param> public ShopCartItem(string ID, string Name, double Price) { _ID = ID; _Name = Name; _DeliveryName = ""; _Quantity = 1; _Price = Price; _Score = 0; _ProductId = ""; _PicUrl = ""; _MarketPrice = 0; _UserPrice = 0; _VipPrice = 0; _SendPrice = 0; _ShopID = ""; _UrlFrom = ""; } /// <summary> /// 购物车商品项构架函数 /// </summary> /// <param name="ID">产品ID</param> /// <param name="Name">产品名称</param> /// <param name="Price">产品单价(计算价)</param> /// <param name="UrlFrom">页面来源</param> public ShopCartItem(string ID, string Name, double Price,string UrlFrom) { _ID = ID; _Name = Name; _DeliveryName = ""; _Quantity = 1; _Price = Price; _Score = 0; _ProductId = ""; _PicUrl = ""; _MarketPrice = 0; _UserPrice = 0; _VipPrice = 0; _SendPrice = 0; _ShopID = ""; _UrlFrom = UrlFrom; } /// <summary> /// 购物车商品项标准构架函数 /// </summary> /// <param name="ID">产品ID</param> /// <param name="Name">产品名称</param> /// <param name="Price">产品单价(计算价)</param> /// <param name="UrlFrom">页面来源</param> /// <param name="UrlFrom">页面来源</param> public ShopCartItem(string ID, string Name, double Price,string ShopID, string UrlFrom) { _ID = ID; _Name = Name; _DeliveryName = ""; _Quantity = 1; _Price = Price; _Score = 0; _ProductId = ""; _PicUrl = ""; _MarketPrice = 0; _UserPrice = 0; _VipPrice = 0; _SendPrice = 0; _ShopID = ShopID; _UrlFrom = UrlFrom; } }
0
相关文章