T
TRB_NV
I'd been using an Access database based shopping cart, but wanted to change
it so that it would use session variables. I have a form that's submitted
to a page called addtocart.asp that contains the following information:
intProdID -- ProductID
strProdName -- Product Name
intQuant -- Quantity
intProdPrice -- Price
productType -- Type of product (ie. Wine, Cheese, etc...)
I also made the cart be able to process more than one item at a time. For
example, I had an event where customers could specify how many Wine or
Cheese tickets they'd like to purchase. They select the amounts and hit
submit, which takes them to the addtocart.asp page. I left a bunch of
trouble shooting items REM'd out. They were useful when I was designing the
cart and since it took me a while to get to work perfectly, I wanted to
share it with the public. If you want more details, email me directly --
just be aware, I typically only check this hotmail account once or twice a
month.
Once the info is stored in the Session variables, they're redirected to the
ViewCart.asp page:
<%@ LANGUAGE="VBSCRIPT" %>
<%
''addtocart.asp
''response.write Request.Form & "<br><br>"
if Request.Form("action") = "add" then
count = 0
for each item in Request.form("intProdID")
'Main Program
intProdID = split(Request.Form("intProdID"), ",")
strProdName = split(Request.Form("strProdName"), ",")
intQuant = split(Request.Form("intQuant"), ",")
intProdPrice = split(Request.Form("intProdPrice"), ",")
productType = split(Request.Form("productType"), ",")
clearcart = Request.Form("clear")
if clearcart = "true" then
Session("ItemCount") = 0
clearcart = ""
end if
''response.write intQuant(count) & "<br>"
if trim(Cint(intQuant(count))) > 0 then
intItemCount = Session("ItemCount")
if intItemCount = 0 then
intItemCount = 1
Session("ItemCount") = intItemCount
else
intItemCount = intItemCount + 1
Session("ItemCount") = intItemCount
end if
strProd = "Prod_" & Cstr(intItemCount)
strQnty = "Qnty_" & Cstr(intItemCount)
strDesc = "Desc_" & Cstr(intItemCount)
strCost = "Cost_" & Cstr(intItemCount)
strType = "Type_" & Cstr(intItemCount)
Session("ItemCount") = intItemCount
Session(strProd) = trim(intProdID(count))
Session(strQnty) = trim(Cint(intQuant(count)))
Session(strDesc) = trim(strProdName(count))
Session(strCost) = trim(intProdPrice(count))
Session(strType) = trim(productType(count))
''response.write count & ": Prod:" & intProdID(count) & " Name:" &
strProdname(count) & " Qnty:" & intQuant(count) & " Cost:" &
intProdPrice(count) & " Type:" & productType(count) & "<br>"
end if ''intQuant(count) <> 0
''response.write intItemCount & ": Prod:" & Session(strProd) & " Name:" &
Session(strDesc) & " Qnty:" & Session(strQnty) & " Cost:" & Session(strCost)
& " Type:" & Session(strType) & "<br>"
count = count + 1
next
''response.write "<br>Cart:<br>"
''for intItemCount = 1 to Session("ItemCount")
'' response.write intItemCount & ": Prod:" & Session("Prod_" &
Cstr(intItemCount)) & " Name:" & Session("Desc_" & Cstr(intItemCount)) & "
Qnty:" & Session("Qnty_" & Cstr(intItemCount)) & " Cost:" & Session("Cost_"
& Cstr(intItemCount)) & " Type:" & Session("Type_" & Cstr(intItemCount)) &
"<br>"
''next
''response.end
%>
<%@ LANGUAGE="VBSCRIPT" %>
<!--Start Basket Contents-->
<%
''ViewCart.asp
if intItemCount <> 0 then
intShipping = 0
intService = 0
intSubTotal = 0
intTotal = 0
strShipMethod = "No Tax"
for count = 1 to intItemCount
strProd = "Prod_" & Cstr(count)
strQnty = "Qnty_" & Cstr(count)
strDesc = "Desc_" & Cstr(count)
strCost = "Cost_" & Cstr(count)
strType = "Type_" & Cstr(count)
intSubTotal = intSubTotal + (Cint(Session(strQnty)) *
Cint(Session(strCost)))
intInventory = Cint(999)
if intInventory < Cint(Session(strQnty)) then status = "<font
color=red>On-Order</font>"
if intInventory >= Cint(Session(strQnty)) then status = "In-Stock"
strShipMethod = "NJ"
if strShipMethod = "NJ" then intTaxRate = .06 else intTaxRate = .00
intTax = round (((intSubTotal) * intTaxRate), 2)
intTotal = round ((intSubTotal + intShipping + intService + intTax), 2)
%>
<tr><form action="checkout.asp" method="post" id="form1" name="form1">
<td width="10%" align="left"><font face="Arial" size="1"> <%=
Session(strProd) %></font></td>
<td width="50%" align="left"><font face="Arial" size="1"> <%=
Session(strDesc) %></font></td>
<td width="10%" align="right"><font face="Arial" size="1"> $<%=
formatNumber(Session(strCost), 2) %>  </font></td>
<td width="10%" align="center"><font face="Arial" size="1"> <%=
Session(strQnty) %></font></td>
<td width="10%" align="right"><font face="Arial" size="1"> $<%=
formatNumber((Session(strQnty) * Session(strCost)), 2) %></font></td>
<td width="15%" align="center"><font face="Arial" size="1"> <%= status
%></font></td>
<td><input name="intItemID" type="hidden" value="<%= count %>">
<input name="intProdID" type="hidden" value="<%=
Session(strProd) %>">
<input name="strProdName" type="hidden" value="<%=
Session(strDesc) %>">
<input name="intProdPrice" type="hidden" value="<%=
Session(strCost) %>">
<input name="intQuant" type="hidden" value="<%=
Session(strQnty) %>">
<input type="submit" name="control" value="Delete"></td>
</form>
</tr>
<%
next
else
response.write "<b>Cart is empty</b>"
end if
%>
<!--End Basket Contents-->
it so that it would use session variables. I have a form that's submitted
to a page called addtocart.asp that contains the following information:
intProdID -- ProductID
strProdName -- Product Name
intQuant -- Quantity
intProdPrice -- Price
productType -- Type of product (ie. Wine, Cheese, etc...)
I also made the cart be able to process more than one item at a time. For
example, I had an event where customers could specify how many Wine or
Cheese tickets they'd like to purchase. They select the amounts and hit
submit, which takes them to the addtocart.asp page. I left a bunch of
trouble shooting items REM'd out. They were useful when I was designing the
cart and since it took me a while to get to work perfectly, I wanted to
share it with the public. If you want more details, email me directly --
just be aware, I typically only check this hotmail account once or twice a
month.
Once the info is stored in the Session variables, they're redirected to the
ViewCart.asp page:
<%@ LANGUAGE="VBSCRIPT" %>
<%
''addtocart.asp
''response.write Request.Form & "<br><br>"
if Request.Form("action") = "add" then
count = 0
for each item in Request.form("intProdID")
'Main Program
intProdID = split(Request.Form("intProdID"), ",")
strProdName = split(Request.Form("strProdName"), ",")
intQuant = split(Request.Form("intQuant"), ",")
intProdPrice = split(Request.Form("intProdPrice"), ",")
productType = split(Request.Form("productType"), ",")
clearcart = Request.Form("clear")
if clearcart = "true" then
Session("ItemCount") = 0
clearcart = ""
end if
''response.write intQuant(count) & "<br>"
if trim(Cint(intQuant(count))) > 0 then
intItemCount = Session("ItemCount")
if intItemCount = 0 then
intItemCount = 1
Session("ItemCount") = intItemCount
else
intItemCount = intItemCount + 1
Session("ItemCount") = intItemCount
end if
strProd = "Prod_" & Cstr(intItemCount)
strQnty = "Qnty_" & Cstr(intItemCount)
strDesc = "Desc_" & Cstr(intItemCount)
strCost = "Cost_" & Cstr(intItemCount)
strType = "Type_" & Cstr(intItemCount)
Session("ItemCount") = intItemCount
Session(strProd) = trim(intProdID(count))
Session(strQnty) = trim(Cint(intQuant(count)))
Session(strDesc) = trim(strProdName(count))
Session(strCost) = trim(intProdPrice(count))
Session(strType) = trim(productType(count))
''response.write count & ": Prod:" & intProdID(count) & " Name:" &
strProdname(count) & " Qnty:" & intQuant(count) & " Cost:" &
intProdPrice(count) & " Type:" & productType(count) & "<br>"
end if ''intQuant(count) <> 0
''response.write intItemCount & ": Prod:" & Session(strProd) & " Name:" &
Session(strDesc) & " Qnty:" & Session(strQnty) & " Cost:" & Session(strCost)
& " Type:" & Session(strType) & "<br>"
count = count + 1
next
''response.write "<br>Cart:<br>"
''for intItemCount = 1 to Session("ItemCount")
'' response.write intItemCount & ": Prod:" & Session("Prod_" &
Cstr(intItemCount)) & " Name:" & Session("Desc_" & Cstr(intItemCount)) & "
Qnty:" & Session("Qnty_" & Cstr(intItemCount)) & " Cost:" & Session("Cost_"
& Cstr(intItemCount)) & " Type:" & Session("Type_" & Cstr(intItemCount)) &
"<br>"
''next
''response.end
%>
<%@ LANGUAGE="VBSCRIPT" %>
<!--Start Basket Contents-->
<%
''ViewCart.asp
if intItemCount <> 0 then
intShipping = 0
intService = 0
intSubTotal = 0
intTotal = 0
strShipMethod = "No Tax"
for count = 1 to intItemCount
strProd = "Prod_" & Cstr(count)
strQnty = "Qnty_" & Cstr(count)
strDesc = "Desc_" & Cstr(count)
strCost = "Cost_" & Cstr(count)
strType = "Type_" & Cstr(count)
intSubTotal = intSubTotal + (Cint(Session(strQnty)) *
Cint(Session(strCost)))
intInventory = Cint(999)
if intInventory < Cint(Session(strQnty)) then status = "<font
color=red>On-Order</font>"
if intInventory >= Cint(Session(strQnty)) then status = "In-Stock"
strShipMethod = "NJ"
if strShipMethod = "NJ" then intTaxRate = .06 else intTaxRate = .00
intTax = round (((intSubTotal) * intTaxRate), 2)
intTotal = round ((intSubTotal + intShipping + intService + intTax), 2)
%>
<tr><form action="checkout.asp" method="post" id="form1" name="form1">
<td width="10%" align="left"><font face="Arial" size="1"> <%=
Session(strProd) %></font></td>
<td width="50%" align="left"><font face="Arial" size="1"> <%=
Session(strDesc) %></font></td>
<td width="10%" align="right"><font face="Arial" size="1"> $<%=
formatNumber(Session(strCost), 2) %>  </font></td>
<td width="10%" align="center"><font face="Arial" size="1"> <%=
Session(strQnty) %></font></td>
<td width="10%" align="right"><font face="Arial" size="1"> $<%=
formatNumber((Session(strQnty) * Session(strCost)), 2) %></font></td>
<td width="15%" align="center"><font face="Arial" size="1"> <%= status
%></font></td>
<td><input name="intItemID" type="hidden" value="<%= count %>">
<input name="intProdID" type="hidden" value="<%=
Session(strProd) %>">
<input name="strProdName" type="hidden" value="<%=
Session(strDesc) %>">
<input name="intProdPrice" type="hidden" value="<%=
Session(strCost) %>">
<input name="intQuant" type="hidden" value="<%=
Session(strQnty) %>">
<input type="submit" name="control" value="Delete"></td>
</form>
</tr>
<%
next
else
response.write "<b>Cart is empty</b>"
end if
%>
<!--End Basket Contents-->