cookie版购物车

添加商品到购物车的方法

#region 添加到购物车AddShoppingCar
/// <summary>
/// 添加到购物车AddShoppingCar
/// </summary>
/// <param name="num">数量 如果存在产品 负数是减少 正数是增加  如果不存在  直接增加</param>
/// <param name="id">货物ID</param>
/// <param name="expires">cookies保存的天数</param>
public static void AddShoppingCar(string num, string id, int expires)
{
    if (System.Web.HttpContext.Current.Request.Cookies["Products"] != null)
    {
        System.Web.HttpCookie cookie;
        string cookievalue = System.Web.HttpContext.Current.Request.Cookies["Products"].Value;
        if (System.Web.HttpContext.Current.Request.Cookies["Products"].Values[id] == null)
        {
            cookievalue = cookievalue + "&" + id + "=" + num;

        }
        else
        {
            int num1 = int.Parse(System.Web.HttpContext.Current.Request.Cookies["Products"].Values[id].ToString()) + int.Parse(num);
            if (num1 > 0)
            {
                System.Web.HttpContext.Current.Request.Cookies["Products"].Values[id] = num1.ToString();
            }
            else
            {
                System.Web.HttpContext.Current.Request.Cookies["Products"].Values[id] = "0";
            }
            cookievalue = System.Web.HttpContext.Current.Request.Cookies["Products"].Value;
        }
        cookie = new System.Web.HttpCookie("Products", cookievalue);
        if (expires != 0)
        {
            DateTime dt = DateTime.Now;
            TimeSpan ts = new TimeSpan(expires, 0, 0, 20);
            cookie.Expires = dt.Add(ts);
        }
        System.Web.HttpContext.Current.Response.AppendCookie(cookie);
    }
    else
    {
        System.Web.HttpCookie newcookie = new HttpCookie("Products");
        if (expires != 0)
        {
            DateTime dt = DateTime.Now;
            TimeSpan ts = new TimeSpan(expires, 0, 0, 20);
            newcookie.Expires = dt.Add(ts);
        }
        newcookie.Values[id] = num;
        System.Web.HttpContext.Current.Response.AppendCookie(newcookie);
    }
}
#endregion

删除产品的方法 (仅仅是使商品数量为0)

#region 根据ID删除产品RemoveShoppingCar
/// <summary>
/// 根据ID删除产品RemoveShoppingCar
/// </summary>
/// <param name="id">产品ID</param>
public static void RemoveShoppingCar(string id)
{
    if (System.Web.HttpContext.Current.Request.Cookies["Products"] != null && System.Web.HttpContext.Current.Request.Cookies["Products"].Values[id] != null)
    {
        System.Web.HttpCookie cookie;
        System.Web.HttpContext.Current.Request.Cookies["Products"].Values[id] = "0";
        string cookievalue = System.Web.HttpContext.Current.Request.Cookies["Products"].Value;
        cookie = new System.Web.HttpCookie("Products", cookievalue);
        System.Web.HttpContext.Current.Response.AppendCookie(cookie);
    }
}
#endregion

修改产品数量

#region 根据ID修改产品UpdateShoppingCar
/// <summary>
/// 根据ID修改产品UpdateShoppingCar
/// </summary>
/// <param name="id">产品ID</param>
public static void UpdateShoppingCar(string id,string num)
{
    if (System.Web.HttpContext.Current.Request.Cookies["Products"] != null && System.Web.HttpContext.Current.Request.Cookies["Products"].Values[id] != null)
    {
        System.Web.HttpCookie cookie;
        System.Web.HttpContext.Current.Request.Cookies["Products"].Values[id] = num;
        string cookievalue = System.Web.HttpContext.Current.Request.Cookies["Products"].Value;
        cookie = new System.Web.HttpCookie("Products", cookievalue);
        System.Web.HttpContext.Current.Response.AppendCookie(cookie);
    }
}
#endregion

获取列表

#region 得到所有的产品列表GetAllChoppingCar
/// <summary>
/// 得到所有的产品列表GetAllChoppingCar
/// </summary>
/// <returns>DataTable</returns>
public static DataTable GetAllChoppingCar()
{
    if (System.Web.HttpContext.Current.Request.Cookies["Products"] != null)
    {
        int count = System.Web.HttpContext.Current.Request.Cookies["Products"].Values.Count;
        DataColumn dcid = new DataColumn("id");
        DataColumn dcnum = new DataColumn("num");
        DataTable dt = new DataTable();
        dt.Columns.Add(dcid);
        dt.Columns.Add(dcnum);

        string[] str = System.Web.HttpContext.Current.Request.Cookies["Products"].Value.Split('&');
        for (int i = 0; i < str.Length; i++)
        {
            DataRow dr = dt.NewRow();
            dr["id"] = (str[i].Split('='))[0].ToString();
            dr["num"] = (str[i].Split('='))[1].ToString();
            if (int.Parse((str[i].Split('='))[1].ToString()) != 0)
            {
                dt.Rows.Add(dr);
            }
        }
        return dt;
    }
    else
    {
        return null;
    }
}
#endregion

posted on 2009-02-14 10:47  草原和大树  阅读(920)  评论(0编辑  收藏  举报