Skip to main content

Shopping Cart Example in ASP.NET


using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


public class ShoppingCart
{
    public ShoppingCart()
    {
    }
    public DataTable AddItem(int ProductID, string Weight, int Quantity)
    {
        DataTable dt = new DataTable();
        DataRow dtRow;
        string Title = "", Description = "";
        double UnitPrice = 0;
        dt = GetCurrentDetails();
        bool blnMatch = false;

        if (dt != null)
        {
            foreach (DataRow objDR_loopVariable in dt.Rows)
            {
                dtRow = objDR_loopVariable;
                if (Convert.ToInt32(dtRow["ProductId"]) == ProductID && dtRow["Weight"].ToString() == Weight)
                {
                    dtRow["Quantity"] = (Convert.ToDouble(dtRow["Quantity"]) + Convert.ToDouble(Quantity)).ToString();
                    blnMatch = true;
                    break;
                }
            }
        }
        if (!blnMatch)
        {
            dtRow = dt.NewRow();
             /*
            objPL_Product.Product_id = ProductID;
            objPL_Product.Weight = Weight;
            DataTable dtGetProduct = objBL_Product.GetProduct_ID_Weight();
            if (dtGetProduct.Rows.Count > 0)
            {
                Title = dtGetProduct.Rows[0]["product_name"].ToString();
                Description = GetShortDescription(dtGetProduct.Rows[0]["product_decription"].ToString());
                UnitPrice = Convert.ToDouble(dtGetProduct.Rows[0]["price_sale"].ToString());
            }*/

             //Put data
            dtRow["ProductId"] = ProductID;//
            dtRow["Title"] = Title;
            dtRow["Weight"] = Weight;//
            dtRow["Description"] = Description;
            dtRow["Quantity"] = Quantity;//
            dtRow["UnitPrice"] = UnitPrice;
            //dtRow["TotalPrice"] = GetPrice();
            dt.Rows.Add(dtRow);
        }
        HttpContext.Current.Session["ShoppingCart"] = dt;

        return dt;
    }
    public double GetTotal()
    {
        DataTable dt = GetCurrentDetails();
        double total = 0;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            total = total + (Convert.ToDouble(dt.Rows[i]["Quantity"]) * Convert.ToDouble(dt.Rows[i]["UnitPrice"]));
        }
        return total;
    }
    public DataTable GetCurrentDetails()
    {
        DataTable dtData = new DataTable();
        if (HttpContext.Current.Session["ShoppingCart"] != null)
        {
            dtData = (DataTable)HttpContext.Current.Session["ShoppingCart"];
        }
        else
        {
            dtData.Columns.Add("ProductId");
            dtData.Columns.Add("Title");
            dtData.Columns.Add("Weight");
            dtData.Columns.Add("Description");
            dtData.Columns.Add("Quantity");
            dtData.Columns.Add("UnitPrice");
            dtData.Columns.Add("TotalPrice");

            HttpContext.Current.Session["ShoppingCart"] = dtData;
        }
        return dtData;
    }
    public int CountItem()
    {
        return ((DataTable)HttpContext.Current.Session["ShoppingCart"]).Rows.Count;
    }
    public string GetShortDescription(object obj)
    {
        string s = "";
        try
        {
            s = Function.GetTextonly(obj.ToString()) + "...";
            s = Function.GetTextonly(obj.ToString()).Substring(0, 150) + "...";
            return s;
        }
        catch (Exception ex) { return s; }
    }

    public void RemoveItem(int Product_ID, string weight)
    {
        DataTable dtData = GetCurrentDetails();
        DataRow dtRow;
        foreach (DataRow objDR_loopVariable in dtData.Rows)
        {
            dtRow = objDR_loopVariable;
            if (Convert.ToInt32(dtRow["ProductId"]) == Product_ID && dtRow["Weight"].ToString() == weight)
            {
                dtRow.Delete();
                break;
            }
        }

        HttpContext.Current.Session["ShoppingCart"] = dtData;
    }
    public void SetItemQuantity(int Product_ID, string Weight, int Quantity)
    {
        DataTable dtData = GetCurrentDetails();
        DataRow dtRow;
        foreach (DataRow objDR_loopVariable in dtData.Rows)
        {
            dtRow = objDR_loopVariable;
            if (Convert.ToInt32(dtRow["ProductId"]) == Product_ID && dtRow["Weight"].ToString() == Weight)
            {
                dtRow["Quantity"] = Quantity.ToString();
                break;
            }
        }

        HttpContext.Current.Session["ShoppingCart"] = dtData;
    }
}


------------------------------------------------------------

Methods :
AddItem(int ProductID, string Weight, int Quantity)
GetTotal()
GetCurrentDetails()
CountItem()
RemoveItem(int Product_ID, string weight)
SetItemQuantity(int Product_ID, string Weight, int Quantity)

Comments