Code for a working shopping cart solution in classic ASP
In this article we will be looking at the code for a complete shopping cart solution implemented in ASP.
In order to get this working we will have to configure a local instance of an ADODB database.
Start -> Run -> odbcad32
Configure the System DSN depending on your database of choice. We will create product and cart tables. The fields can be determined by glancing over the code.
Lets get started...
File: catlist.asp
File: databaseCart.asp
File: default.asp
Welcome to the Apple Store
<%
'get current category
cat = trim(Request("cat"))
if cat="" then cat="Home"
'Open the database connection
set con = Server.CreateObject("ADODB.Connection")
Con.Open "asp"
%>
<%If cat="Home" then%>
<% else%>
<%end if%>
www.5ynth3t1c1nt31.com
Welcome to the Apple Store
File: product.asp
<%
'get the product id
dim productID
dim SqlString
dim cat
productID = Trim(request("pid"))
'Open the database connection
Set con = Server.CreateObject("ADODB.Connection")
Con.Open "asp"
'Get the product information
sqlString = "select * from asp.dbo.products where product_id='" & productId & "'"
Set rs = Server.CreateObject("ADODB.RecordSet")
Rs.ActiveConnection = con
rs.open SqlString
'get current category
cat = RS("category")
%>
www.5ynth3t1c1nt31.com
File: productList.asp
<%
set prodRS = Server.CreateObject("ADODB.RecordSet")
prodRS.ActiveConnection = con
sqlString = "select * from asp.dbo.products where category='" & cat & "'" & " and status=1"
prodRS.Open sqlString
%>
Email Application Session shopping cart example
File: sendMail.asp
<%@ Language=VBScript %>
<%
Set mailObject = Server.CreateObject("CDONTS.NewMail")
rcptStr = "
This e-mail address is being protected from spam bots, you need JavaScript enabled to view it
"
mailObject.Send "
This e-mail address is being protected from spam bots, you need JavaScript enabled to view it
", rcptStr,"Check it out","Here is some email for you"
Set mailObject = Nothing
%>
Sent mail to <%=rcptStr%>
File: sessionCart.asp
<%
'Define constants
CONST CARTPID = 0
CONST CARTPNAME = 1
CONST CARTPQUANTITY = 3
'Get the shopping cart.
if not isArray(session("cart")) then
dim localCart(4,20)
else
localCart = session("cart")
end if
'Get product information
productID = trim(request("pid"))
productName = trim(request("name"))
productPrice = trim(request("price"))
'Add item to the cart
if productID <> "" then
foundIt = false
for i = 0 to ubound(localCart)
if localCart(cartPid,i) = productId then
localCart(CARTPQUANTITY,i) = localCart(CARTPQUANTITY,i)+1
foundIt = true
exit for
end if
next
if not foundIt then
for i = 0 to ubound(localCart,2)
if localCart(CARTPID,i) = "" then
localCart(CARTPID,i) = productId
localCart(CARTPNAME,i) = productName
localCart(CARTPQUANTITY,i) = productQuantity
exit for
end if
next
end if
end if
'Update shopping cart quantities
if request("updateQ") <> "" then
for i = 0 to ubound(localCart,2)
newQ = trim(request("pq" & localCart(CARTPID,i)))
deleteProduct = trim(request("pd" & localCart(CARTPID,i)))
if newQ = "" or newQ = "0" or deleteProduct <> "" then
localCart(CARTPID,i) = ""
end if
if isNumeric(newQ) then
localCart(CARTPQUANTITY,i) = newQ
end if
next
end if
'Update Session variable with the array
Session("cart") = localCart
%>
Your shopping cart
<%
orderTotal = 0
%>
Last Updated ( Wednesday, 18 June 2008 )