472,125 Members | 1,418 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,125 software developers and data experts.

Session based Shopping Cart

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">&nbsp;<%=
Session(strProd) %></font></td>
<td width="50%" align="left"><font face="Arial" size="1">&nbsp;<%=
Session(strDesc) %></font></td>
<td width="10%" align="right"><font face="Arial" size="1"> $<%=
formatNumber(Session(strCost), 2) %> &nbsp</font></td>
<td width="10%" align="center"><font face="Arial" size="1">&nbsp;<%=
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-->
May 26 '06 #1
0 2289

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Adil Akram | last post: by
4 posts views Thread by Adil Akram | last post: by
2 posts views Thread by Paul Hobbs | last post: by
2 posts views Thread by Joe Molloy | last post: by
1 post views Thread by Adil Akram | last post: by
7 posts views Thread by Joseph Byrns | last post: by
4 posts views Thread by Nick Gilbert | last post: by
5 posts views Thread by rahia307 | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.