<%
Sub AddItemToCart(iItemID, iItemCount)
If dictCart.Exists(iItemID) Then
dictCart(iItemID) = dictCart(iItemID) + iItemCount
Else
dictCart.Add iItemID, iItemCount
End If
Response.Write "" & iItemCount & " of item # " & iItemID & " have been added to your cart.
" & vbCrLf
End Sub
Sub RemoveItemFromCart(iItemID, iItemCount)
If dictCart.Exists(iItemID) Then
If dictCart(iItemID) <= iItemCount Then
dictCart.Remove iItemID
Else
dictCart(iItemID) = dictCart(iItemID) - iItemCount
End If
Response.Write "" & iItemCount & " of item # " & iItemID & " have been removed from your cart.
" & vbCrLf
Else
Response.Write "Couldn't find any of that item your cart.
" & vbCrLf
End If
End Sub
Sub ShowItemsInCart()
Dim Key
Dim aParameters ' as Variant (Array)
Dim sTotal, sShipping
%>
Item #
Description
Quantity
Edit
Items In Cart
Price
Totals
<%
sTotal = 0
For Each Key in dictCart
aParameters = GetItemParameters(Key)
%>
<%
sTotal = sTotal + (dictCart(Key) * CSng(aParameters(2)))
Next
'Calculate shipping - you might want to pull this out into a function if your shipping
'calculations are more complicated than ours. ;)
If sTotal <> 0 Then
sShipping = 0.00 '****shipping1 Change this to your shipping rate. Also change it where repeated a page below
Else
sShipping = 0.00
End If
sTotal = sTotal + sShipping
%>
Shipping & Handling:
$ <%= FormatNumber(sShipping,2) %>
Total:
$<%= FormatNumber(sTotal,2) %>
<%
End Sub
Sub ShowFullCatalog()
Dim aParameters ' as Variant (Array)
Dim I
Dim iItemCount ' Number of items you sell
'****itemcount Change the number below to be the amount of items you have listed
iItemCount = 5
%>
Description
Price
Add Item
<%
For I = 1 to iItemCount
aParameters = GetItemParameters(I)
%>
<%= aParameters(1) %>
<%= aParameters(3) %>
$<%= aParameters(2) %>
<%
Next 'I
%>
<%
End Sub
Sub PlaceOrder()
Dim Key
Dim aParameters ' as Variant (Array)
Dim sTotal, sShipping
%>
Item #
Description
Quantity
Price
Totals
<%
For I = 1 to iItemCount
aParameters = GetItemParameters(I)
%>
<%= aParameters(1) %>
<%
Next 'I
%>
Please review the items in your cart above. Use the "Edit
Your Order" button below if you need to make any changes.
If you are ready to place this order, please fill-out the form
below with your information. Once completed, click
on the "Click here to make to make secure payment"
button below. Your credit card informations is safely processed
on our secure server. ( A symbol of a padlock
on your browser will change from an unlocked position to a locked
position.)
<%
Dim sales, salesnoship, txRate, notaxrate, salesplustax, justtax, grandtotal, grandnotax, statename, statecode
sales = FormatNumber(sTotal,2) ' All items plus shipping and no tax
salesnoship = ((sTotal) - sShipping) ' All items without shipping nor tax
txRate = 0 ' ****tax Change this to your state tax rate
notaxrate = 0.00 ' No tax
salesplustax = (((salesnoship/100) * txRate) + salesnoship) ' This is sales plus tax
justtax = ((salesnoship/100) * txRate) ' This is the tax on all items in cart
grandtotal = ((salesplustax) + sShipping) ' This is sales plus tax plus shipping
grandnotax = ((salesnoship) + sShipping) ' This is sales plus shipping
statename = "Massachusetts" ' ****state1 Change this to your state's full name
statecode = "MA"' ' ****state2 Change this to your state's 2 letter code
%>
No Sales Tax
<%
' Declare our variable
Dim iChoice
' Read in the choice the user clicked on.
' This will equal "" (an empty string) if no choice is there
iChoice = Request.QueryString("choice")
' Execute the appropriate branch based upon the value of the variable we just read in.
Select Case iChoice
Case "1"
%>
Item
Total
$<%= FormatNumber(salesnoship,2) %>
<%= txRate %>% <%= statecode %> Sales Tax
$<%= FormatNumber(justtax,2) %>
Shipping
$<%= FormatNumber(sShipping,2) %>
Grand
Total
$<%= FormatNumber(grandtotal,2) %>
<%
Case "2"
%>
This order is not shipping to <%= statename %>. No sales tax will
be added to your order.
Item
Total
$<%= FormatNumber(salesnoship,2) %>
Sales Tax
$<%= FormatNumber(notaxrate,2) %>
Shipping
$<%= FormatNumber(sShipping,2) %>
Grand Total
$<%= FormatNumber(grandnotax,2) %>
<%
Case "3"
%>
<%
Case Else
' This executes if no of the other conditions are met!
' This actually runs the first time through when the user
' arrives at this page because the choice is blank.
End Select
' You might also notice that in the above cases I was comparing the value of iChoice
' to strings containing numbers and not to the actual numerical values 1, 2, and 3.
' If I wanted to use the numerical values I should technically convert iChoice to a
' number before doing the comparison because 3 is really not the same as "3". VBScript
' will let you get away with it occasionally, but it's good practice to actually do the
' conversion and, as an additional benefit, doing so will also help avoid confusion
' when people are reading your code. CInt or CLng is the appropriate command:
'
'Select Case CInt(iChoice)
' Case 1
' ...
' Case 2
' ...
' ...
'End Select
%>
Information
Full Name
Shipping Address
City
State / Prov
Zip Code / Postal
Country
Email Address
<%
End Sub
' ****products Below is where you add your items
' We implemented this this way so if you attach it to a database you'd only need one call per item
' Change ***itemcount to reflect the number of items below
Function GetItemParameters(iItemID)
Dim aParameters ' Will contain 4 string values : image path, short title, price and description
' However we need to keep price so it can be converted to a
' single for computation hence no currency symbol. This array
' can also be expanded to contain any other information about
' product that you might want to pull from the DB.
Select Case iItemID
Case 1
aParameters = Array("products/100-400.jpg", "Undergraduate", "400.00", "Required for Undergraduate Delegates. Non-delegate undergrads may also purchase this option. Covers four nights accommodations, Welcome Reception, four breakfasts, Thursday and Saturday lunches, Business Sessions, Omega Service, Friday Virgin Islands Reception, Saturday Springs Preserve Tour and Professor Dorgan's powerpoint lecture on Las Vegas, PGL Reception and Grand Banquet.")
Case 2
aParameters = Array("products/100-400.jpg", "Alumni", "350.00", "Covers Thursday and Saturday lunches, Business Sessions, Omega Service, Friday Alumni Seminar, Friday Virgin Islands Reception, Professor Dorgan's Saturday powerpoint lecture on Las Vegas, PGL Reception and Grand Banquet.")
Case 3
aParameters = Array("products/100-400.jpg", "Joint with Spouse or Partner", "600.00", "Covers (for 2): Thursday and Saturday lunches, Business Sessions, Omega Service, Friday Alumni Seminar, Friday Virgin Islands Reception, Professor Dorgan's Saturday powerpoint lecture on Las Vegas, PGL Reception and Grand Banquet.")
Case 4
aParameters = Array("products/100-400.jpg", "Virgin Islands Party single", "50.00", "Virgin Islands Party Single Ticket.")
Case 5
aParameters = Array("products/100-400.jpg", "Grand Banquet single", "75.00", "Grand Banquet Single Ticket")
End Select
' Return array containing product info.
GetItemParameters = aParameters
End Function
%>
<% ' * Begin the infamous runtime script *
' Declare our Vars
Dim dictCart ' as dictionary
Dim sAction ' as string
Dim iItemID ' as integer
Dim iItemCount ' as integer
' Get a reference to the cart if it exists otherwise create it
If IsObject(Session("cart")) Then
Set dictCart = Session("cart")
Else
' We use a dictionary so we can name our keys to correspond to our
' item numbers and then use their value to hold the quantity. An
' array would also work, but would be a little more complex and
' probably not as easy for readers to follow.
Set dictCart = Server.CreateObject("Scripting.Dictionary")
End If
' Get all the parameters passed to the script
sAction = CStr(Request.QueryString("action"))
iItemID = CInt(Request.QueryString("item"))
iItemCount = CInt(Request.QueryString("count"))
%>
<%
' Select action based on user input
Select Case sAction
Case "add"
AddItemToCart iItemID, iItemCount
ShowItemsInCart
%>
<%
Case "del"
RemoveItemFromCart iItemID, iItemCount
ShowItemsInCart
%>
<%
Case "viewcart"
ShowItemsInCart
%>
<%
Case "checkout"
PlaceOrder
%>
<%
Case Else ' Shop
ShowFullCatalog
%>
<%
End Select
' Return cart to Session for storage
Set Session("cart") = dictCart
%>