%@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 %>
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 %>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 %>