<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%> <% 'This code is used to create a shopping cart array in the memory of the server 'The user will actually use a local version that is stored in the session 'This code assumes that the page already has a server-side include pointing to a file 'that contains all of the database connection information 'Below is a list of constants (in capitals) and local variables used in the page CONST CART_COLUMNS = 4 ' 4 indicates the amount of fields in each record CONST ITEMID = 1 CONST ITEMNAME = 2 CONST ITEMPRICE = 3 CONST ITEMQUANTITY = 4 ' /* *************************************************** */ Dim qty Dim buyItem qty = Request.QueryString("qty") 'Must match the field name for quantity from detail page buyItem = Request.QueryString("itemID") 'Must match the field name for itemID from the previous page ' /* *************************************************** */ Dim cartMaxUsed 'The number of items we have added to the array Dim localCart 'This is the local variable to hold the contents of the shopping cart array Dim cartRows 'This local variable is the number of rows the array has, by default 4 (0 to 3) Dim IDsession ' Used locally to display the sessionID IDsession = Session.SessionID 'Populates the local variable with the session id generated by the server Dim nSumSubtotals 'The net of all items in the cart Dim shipping 'The shipping cost - currently $1.99 shipping = FormatCurrency(CDbl(1.99), 2) Dim orderTotal 'The total of the entire order including shipping Dim fAddProductNeeded ' This is a variable to determine if a new row is needed for a product Dim cartEmpty'This is a local variable to display if the cart is empty cartEmpty = "The cart is currently empty." Dim noCart 'Hold an error message if the user enters without a cart being created noCart = "" 'Sets the variable to nothing Session("noCart") = noCart 'Saves it to a session 'Get the values from the session and store them locally cartRows = Session("cartRows")'This is the number of rows in the array localCart = Session("cartArray")'This is the entire array stored locally cartMaxUsed = Session("cartMaxUsed") 'The number of items we have added to the array 'The code to create a new array is within an else statement at the bottom of the page 'Test to see if we have any items in the array if cartMaxUsed <> "" then 'Creates a connection object Set conAcme = Server.CreateObject("ADODB.Command") 'Enlarges the number of rows in the array if needed If (buyItem) <> "" Then 'Tests to see if the items in the cart is the same as the number of rows in the cart If cartMaxUsed = cartRows Then 'increases the size of the cart by 1 row ReDim Preserve localCart(CART_COLUMNS, cartMaxUsed + 1) 'In VBScript dynamica arrays can only be incresed in the 2nd dimension - see http://www.devguru.com/technologies/vbscript/quickref/redim.html 'stores the increase in size back to the local variable Session("cartRows") = cartMaxUsed + 1 End if 'Checks to see if an item already exists in the array, if yes then increase the quantity instead of adding the item again fAddProductNeeded = True For i = 1 to cartMaxUsed If (localCart(ITEMID,i) = CLng(buyItem)) Then fAddProductNeeded = False localCart(ITEMQUANTITY,i) = localCart(ITEMQUANTITY,i) + CLng(qty) End If Next 'If the item doesn't already exist, then adds the item to the array If (True = fAddProductNeeded) Then ' /* ************* Edit the next two rows to use your database connection object ********** */ conAcme.ActiveConnection = MM_conAcme_STRING strSQL = "SELECT * FROM acme1230.inventory WHERE itemID=" & CLng(buyItem) conAcme.CommandText = strSQL conAcme.Prepared = true Set rsItem = conAcme.Execute'This is a recordset named rsItem based on the value of strSQL If Not rsItem.EOF Then cartMaxUsed = cartMaxUsed + 1 Session("cartMaxUsed") = cartMaxUsed localCart(ITEMID,cartMaxUsed) = rsItem("itemID") localCart(ITEMNAME,cartMaxUsed) = rsItem("itemName") localCart(ITEMPRICE,cartMaxUsed) = rsItem("itemPrice") localCart(ITEMQUANTITY,cartMaxUsed) = qty Session("cartArray") = localCart rsItem.Close() Set rsItem = nothing ' clears the recordset End If End If End If 'Determines what is happening after the item is entered, the array is displayed and the user clicks on a button Select Case Request("Action") Case "Continue Shopping" recalcCart localCart, cartMaxUsed Response.Redirect("/acme/products.asp") Case "Recalculate" recalcCart localCart, cartMaxUsed Case "Cancel Order" cartMaxUsed = 0 Session("cartMaxUsed") = cartMaxUsed ' /* ********** Edit the next line to point to your cart page ***** */ Response.Redirect("/acme/cart.asp") Case "Checkout" recalcCart localCart, cartMaxUsed ' /* ********** Edit the next line to point to your checkout page ***** */ Response.Redirect("/acme/checkout.asp") End Select %> Acme

Acme Inc. Shopping Cart

Please review your items carefully. To remove an item turn off the checkmark to the left of the item's name in the "Confirm" column, then click the "Recalculate" button. Thank you for shopping with us.

<% 'The ServerVariables function below performs a call back to this same page %>
"> <% if cartMaxUsed = 0 then 'this occurs only when the cart is empty %> <% else nSumSubtotals = 0 'sets the default value to 0 'this loop goes through the array and displays the information for each item in the array For i = 1 To cartMaxUsed %> <% If (localCart(ITEMPRICE,i)) <> "" Then nSumSubtotals = nSumSubtotals + (CLng(localCart(ITEMPRICE,i)) * CLng(localCart(ITEMQUANTITY,i))) End If Next end if ' This closes the if then else to see if anything is in the cart %> <% ' This shows a shipping charge and order total if there is at least 1 item in the cart If cartMaxUsed >= 1 Then %> <% orderTotal = nSumSubtotals + shipping %> <% End If %>
Confirm Product Name Quantity Unit Price Unit Total
<%= cartEmpty %>
<%=localCart(ITEMNAME,i)%> <%=FormatCurrency(localCart(ITEMPRICE,i))%> <%=FormatCurrency(CLng(localCart(ITEMPRICE,i)) * CLng(localCart(ITEMQUANTITY,i)))%>
  Subtotal: <%=FormatCurrency(nSumSubtotals)%>
  Shipping: <%=shipping %>
  Order Total:<%= FormatCurrency(orderTotal) %>
 
  <% 'This shows these two buttons if there is at least 1 item in the cart If cartMaxUsed >= 1 Then %> <% End If %>
<% 'This subroutine or function is used to recalculate the page value if something changes ********** Dim quantity 'This part calculates the values of the order Sub recalcCart(localCart, cartMaxUsed) For i = 1 to cartMaxUsed quantity = Request("Quantity" & CStr(i)) If IsNumeric(quantity) And quantity > 0 Then localCart(ITEMQUANTITY,i) = Abs(CLng(quantity)) Else localCart(ITEMQUANTITY,i) = 1 End If Next ' This part removes items that are marked to be removed For i = 1 to cartMaxUsed Dim nCuritem 'This indicates which item is being removed Dim theAttributes 'This is the columns If Request("Confirm" & CStr(i)) = "" Then cartMaxUsed = cartMaxUsed - 1 nCurItem = i While nCurItem < Session("cartRows") For theAttributes = 1 to CART_COLUMNS localCart(theAttributes,nCurItem) = localCart(theAttributes, nCurItem + 1) Next nCurItem = nCurItem + 1 Wend End If Next 'Store everything back into the session Session("cartArray") = localCart Session("cartMaxUsed") = cartMaxUsed End Sub '**************** Ends the subroutine ***************************************** else ' This ends the test at the very top of the page by creating an array, setting the error message to the session and sending the user back to the referring page (usually the detail page, or to the catalog page if the user simply came into this page via a search engine or other link and bypassed a page where the array was created CONST CART_ROWS = 3 '3 sets an arbitrary initial number of rows in the array Dim cartArray() ReDim cartArray(CART_COLUMNS,CART_ROWS) cartMaxUsed = 0 'Indicates that we have not used anything yet Session("cartArray") = cartArray 'Names don't have to match, but makes it easier to read Session("cartRows") = CART_ROWS 'Assigns the number of rows to a session variable Session("cartMaxUsed") = cartMaxUsed 'Indicates that no items are in the array yet noCart = "

We apologize, but there was an error, please click the ""Add to cart"" button again.

" Session("noCart") = noCart if Request.ServerVariables("HTTP_REFERER") = "" then ' /* ********** Edit the next line to point to your catalog page ***** */ response.Redirect("/acme/products.asp") else response.Redirect(Request.ServerVariables("HTTP_REFERER")) end if end if %>