9.1.11

Add Cart

namespace Ecommerce.BLL
{
public class AddCart
{


public void BLL_AddItem( Products objBol)
{
ShoppingCart cart = new ShoppingCart();
String cartID = cart.GetShoppingCartId();
Int16 lintCartItemID = 0;

//Declare a datatable to be used as shopping cart
DataTable objDT;
DataRow objDR;

if (System.Web.HttpContext.Current.Session["SessionCart"] == null)
{
//Data table to be used as cart
objDT = new DataTable("Cart");

//Declaring Primary Key For Table
objDT.Columns.Add("ID", System.Type.GetType("System.Int32"));
DataColumn[] ldataColPK = new DataColumn[2];
ldataColPK[0] = objDT.Columns["ID"];
ldataColPK[0].AutoIncrement = true;
ldataColPK[0].AutoIncrementSeed = 1;
objDT.PrimaryKey = ldataColPK;

//Adding columns to datatable
objDT.Columns.Add("CartID", System.Type.GetType("System.String"));
objDT.Columns.Add("ProductID", System.Type.GetType("System.Int32"));
objDT.Columns.Add("Products_Name", System.Type.GetType("System.String"));
objDT.Columns.Add("Model", System.Type.GetType("System.String"));
objDT.Columns.Add("Size", System.Type.GetType("System.String"));
objDT.Columns.Add("Colour", System.Type.GetType("System.String"));
objDT.Columns.Add("ImageUrl", System.Type.GetType("System.String"));
objDT.Columns.Add("Price", System.Type.GetType("System.Double"));
objDT.Columns.Add("Description", System.Type.GetType("System.String"));
objDT.Columns.Add("Quantity", System.Type.GetType("System.Int32"));
objDT.Columns.Add("Stock", System.Type.GetType("System.Int32"));
objDT.Columns.Add("Amount", System.Type.GetType("System.Double"));
objDT.Columns["Amount"].Expression = "Price * Quantity";
}
else
{
objDT = (DataTable)System.Web.HttpContext.Current.Session["SessionCart"];
}

objDR = objDT.NewRow();
objDR["CartID"] = cartID;
objDR["ProductID"] = objBol.ProductsID;
objDR["Products_Name"] = objBol.Products_Name;
objDR["Model"] = objBol.Model;
objDR["Size"] = objBol.attributes_values_name;
objDR["Colour"] = objBol.Colour_name;
objDR["ImageUrl"] = objBol.Icon_image;
objDR["Price"] = objBol.Price;
objDR["Description"] = objBol.Products_Description;
objDR["Quantity"] = objBol.Quantity;
objDR["Stock"] = objBol.Stock;
System.Web.HttpContext.Current.Session["CartItemID"] = objDR["ID"].ToString();
lintCartItemID = Convert.ToInt16(objDR["ID"]);
objDT.Rows.Add(objDR);

//Add the datatable in a session variable
System.Web.HttpContext.Current.Session["SessionCart"] = objDT;

}
=======================================
shopping cart:

public String GetShoppingCartId()
{

// Obtain current HttpContext of ASP+ Request
System.Web.HttpContext context = System.Web.HttpContext.Current;

// If the user is authenticated, use their customerId as a permanent shopping cart id
if (context.User.Identity.Name != "")
{
return context.User.Identity.Name.ToString();
}

// If user is not authenticated, either fetch (or issue) a new temporary cartID
if (context.Request.Cookies["ASPNETCommerce_CartID"] != null)
{
return context.Request.Cookies["ASPNETCommerce_CartID"].Value;
}
else
{
// Generate a new random GUID using System.Guid Class
Guid tempCartId = Guid.NewGuid();

// Send tempCartId back to client as a cookie
context.Response.Cookies["ASPNETCommerce_CartID"].Value = tempCartId.ToString();

// Return tempCartId
return tempCartId.ToString();
}
}

No comments: