<%sPath = "../"%> <% '************************************************************************************ '* * '* MODIFICATION TO THIS SCRIPT OR ANY FILES INCLUDED WITH * '* ONEWORLDSTOREFRONT IS DONE AT YOUR OWN RISK! * '* Product : OneWorldStoreFront * '* Copyright: Copyright (C) 2004 OneWorldStoreFront.Com * '* Registration Number: 1024507 * '* http://strategis.ic.gc.ca/ * '* * '************************************************************************************ '************************************************************************************ '* * '* * '* * '************************************************************************************ Sub ActionSubroutine() Dim aOptions(), aRequired() ReDim aRequired(50, 2) ' we are assuming no more than 50 option groups coming from any one product iNumOptionsSelected = 1 iNumOptions = 1 pClosingCounter = 0 sIDCartRow = 0 sIDProduct = CONVERSION_Numeric(request("idProduct"), C_INTEGER) sQuantity = CONVERSION_Numeric(request("quantity"), C_INTEGER) If ( sQuantity = 0 ) Then sQuantity = 1 End If ' check for bound quantity in cart If ( ( QtySummation() + sQuantity ) >= dOWCartQuantityLimit ) Then Call owMessage(Replace(LANG_DT( "ADDITEM_TOOMANYITEMS", "Too many items in your cart. You may have a maximum of 1: items in you cart before check out. Please delete at least one in order to continue." ), "1:", dOWCartQuantityLimit), C_ERROR) Else sSQLText = "SELECT stock FROM Products WHERE idProduct = " & sIDProduct Call DB_getFromDatabase(sSQLText, rsStoreData, "") If ( NOT rsStoreData.EOF ) Then iStock = rsStoreData("stock") End If Call DB_closeRecordset(rsStoreData) sSQLText = "SELECT * FROM OptionsGroups_products WHERE idProduct = " & sIDProduct Call DB_getFromDatabase(sSQLText, rsStoreData, "") While ( NOT rsStoreData.EOF ) aRequired(iNumOptions ,0) = rsStoreData("Required") ' this is the required flag (Y is yes) aRequired(iNumOptions ,1) = rsStoreData("idOptionGroup") ' this is the idOptionGroup iNumOptions = iNumOptions + 1 rsStoreData.MoveNext Wend Call DB_closeRecordset(rsStoreData) ReDim aOptions(iNumOptions, 4) For Each sOptionSelect In Request.Form If ( InStr(sOptionSelect, "idOption_Dropdown_") > 0 ) Then If ( CONVERSION_Trim(Request(sOptionSelect)) <> "" ) Then aOptions(iNumOptionsSelected, 0) = Replace(sOptionSelect, "idOption_Dropdown_", "") ' this is the idOptionGroup aOptions(iNumOptionsSelected, 1) = CONVERSION_Trim(Request(sOptionSelect)) ' this is the idOption aOptions(iNumOptionsSelected, 2) = "Dropdown" ' this is the data type iNumOptionsSelected = iNumOptionsSelected + 1 End If End If If ( InStr(sOptionSelect, "idOption_Radio_") > 0 ) Then If ( CONVERSION_Trim(Request(sOptionSelect)) <> "" ) Then aOptions(iNumOptionsSelected, 0) = Replace(sOptionSelect, "idOption_Radio_", "") ' this is the idOptionGroup aOptions(iNumOptionsSelected, 1) = CONVERSION_Trim(Request(sOptionSelect)) ' this is the idOption aOptions(iNumOptionsSelected, 2) = "Radio" ' this is the data type iNumOptionsSelected = iNumOptionsSelected + 1 End If End If If ( InStr(sOptionSelect, "idOption_Checkbox_") > 0 ) Then If ( CONVERSION_Trim(Request(sOptionSelect)) <> "" ) Then aOptions(iNumOptionsSelected, 0) = Replace(sOptionSelect, "idOption_Checkbox_", "") ' this is the idOptionGroup aOptions(iNumOptionsSelected, 1) = CONVERSION_Trim(Request(sOptionSelect)) ' this is the idOption aOptions(iNumOptionsSelected, 2) = "Checkbox" ' this is the data type iNumOptionsSelected = iNumOptionsSelected + 1 End If End If If ( InStr(sOptionSelect, "idOption_Text_") > 0 ) Then If ( CONVERSION_Trim(Request(sOptionSelect)) <> "" ) Then aOptions(iNumOptionsSelected, 0) = Replace(sOptionSelect, "idOption_Text_", "") ' this is the idOptionGroup iPos = InStr(aOptions(iNumOptionsSelected, 0), "_") aOptions(iNumOptionsSelected, 1) = Right(aOptions(iNumOptionsSelected, 0), Len(aOptions(iNumOptionsSelected, 0))-iPos) aOptions(iNumOptionsSelected, 0) = Left(aOptions(iNumOptionsSelected, 0), iPos-1) ' must do in this order aOptions(iNumOptionsSelected, 2) = "Text" ' this is the data type aOptions(iNumOptionsSelected, 3) = Trim(Request(sOptionSelect)) ' this is the text data's string iNumOptionsSelected = iNumOptionsSelected + 1 End If End If Next iNumOptionsSelected = iNumOptionsSelected - 1 If ( ( bOWForceToSelectOption ) And ( iNumOptionsSelected = 0 ) And ( iNumOptions > 1 ) ) Then ' the customer is coming from a product listing page and the product they are choosing to add to their cart requires that they add at least one option response.redirect "../owProductDetail.asp?idProduct=" & sIDProduct & "&bSelectOption=T" Else If ( ( bOWForceToSelectOption ) And ( iNumOptionsSelected < iNumOptions-1 ) ) Then ' this product requires that ALL options are selected. Call owMessage(LANG_DT( "ADDITEM_REQUIREDOPTIONS", "Please return and choose a selection for each option this product has." ), C_ERROR) Else If ( sQuantity > dOWMaxCartQty ) Then Call owMessage(Replace(LANG_DT( "ADDITEM_SURPASSSTOREMAX", "The quantity of this product added to your cart exceeds the store maximum of 1: items. Please return and reduce the number added." ), "1:", dOWMaxCartQty), C_ERROR) Else ' must check to see if all the required options have been selected -- this is to make sure if they click the "Add to Cart" ' link from the product listing page that it'll stop them from adding it w/o choosing an option For iX = 1 To iNumOptions If ( aRequired(iX ,0) = "Y" ) Then ' see if it's in the options array and that it's got data bFound = False ' initalize to False at the beginning of each one For iY = 1 To iNumOptionsSelected If ( CStr(Trim(aOptions(iY, 0))) = CStr(Trim(aRequired(iX ,1))) ) And ( CStr(Trim(aOptions(iY, 1))) <> "" ) Then bFound = True End If Next If ( NOT bFound ) Then ' we found one that's not got data, all we needed was one, so redirect back to the product detail page response.redirect "../owProductDetail.asp?idProduct=" & sIDProduct & "&bSelectOption=T" End If End If Next sSQLText = "SELECT * FROM Products WHERE idProduct = " & sIDProduct & " AND Active = -1" Call DB_getFromDatabase(sSQLText, rsStoreData, "") If ( rsStoreData.EOF ) Then Call owMessage(LANG_DT( "ADDITEM_MISSINGITEM", "The product you are adding to your cart is no longer available. Please return and choose another product." ), C_ERROR) Else sUnitPrice = rsStoreData("price") sUnitBtoBPrice = rsStoreData("bToBPrice") sWeight = rsStoreData("weight") sCost = rsStoreData("cost") Call DB_closeRecordset(rsStoreData) ' ***see if the product is in the cart ' check for the product w/ all the options the current product has - no more, no less (should also take into consideration prods w/ no options) If ( iNumOptionsSelected > 0 ) Then sSQLText = "SELECT idCartRow FROM CartProducts WHERE idProduct = " & sIDProduct & " AND idCart = " & sIDCart Call DB_getFromDatabase(sSQLText, rsStoreData, "") If ( NOT rsStoreData.EOF ) Then Do While ( NOT rsStoreData.EOF ) sSQLText = "SELECT Count(*) FROM CartPRoductsOptions WHERE idCartRow = " & rsStoreData("idCartRow") & " AND (" For iX = 1 To iNumOptionsSelected sSQLText = sSQLText & "(idOption = " & aOptions(iX, 1) & " AND idOptionGroup = " & aOptions(iX, 0) & ")" If ( iX < iNumOptionsSelected ) Then sSQLText = sSQLText & "OR" End If Next sSQLText = sSQLText & ")" Call DB_getFromDatabase(sSQLText, rsStoreData2, "") If ( NOT rsStoreData2.EOF ) Then If ( rsStoreData2(0) = iNumOptionsSelected ) Then sIDCartRow = rsStoreData("idCartRow") Exit Do End If End If Call DB_closeRecordset(rsStoreData2) rsStoreData.MoveNext Loop End If Call DB_closeRecordset(rsStoreData) Else ' since there are no options, search for a matching product with no options also sSQLText = "SELECT idCartRow FROM CartProducts WHERE idProduct = " & sIDProduct & " AND idCart = " & sIDCart Call DB_getFromDatabase(sSQLText, rsStoreData, "") If ( NOT rsStoreData.EOF ) Then sIDCartRow = rsStoreData("idCartRow") End If Call DB_closeRecordset(rsStoreData) End If ' ***end checking for product within current cart If ( sIDCartRow = 0 ) Then ' no matching products were found within the cart If ( sQuantity > iStock ) And ( sOWUnderStockBehavior = 0 ) Then Call owMessage(Replace(LANG_DT( "ADDITEM_MAXAMOUNT", "Unfortunately the amount of product you are attempting to add to your cart is more than we have in stock. Please return and choose fewer than 1: to add to your cart." ) ,"1:", iStock), C_ERROR) Else sSQLText = "INSERT INTO CartProducts (idProduct,quantity,unitprice,unitcost,unitweight,unitbtobprice,IDCart) VALUES (" & sIDProduct & "," & sQuantity & "," & sUnitPrice & "," & sCost & "," & sWeight & "," & sUnitBtoBPrice & "," & sIDCart & ")" dbStoreConnection.Execute sSQLText sSQLText ="SELECT Max(CP.idCartRow) FROM CartProducts CP, Cart C WHERE C.idCart = " & sIDCart & " AND C.idCart = CP.idCart" Call DB_getFromDatabase(sSQLText, rsStoreData, "") If ( NOT rsStoreData.EOF ) Then sIDCartRow = rsStoreData(0) End If Call DB_closeRecordset(rsStoreData) For iX = 1 To iNumOptionsSelected pOptionDescrip = "" pPriceToAdd = 0 sSQLText = "SELECT PriceToAdd, OptionDescrip FROM Options WHERE idOption = " & aOptions(iX, 1) Call DB_getFromDatabase(sSQLText, rsStoreData, "") if not rsStoreData.eof then pOptionDescrip = rsStoreData("optionDescrip") pPriceToAdd = rsStoreData("priceToAdd") End If Call DB_closeRecordset(rsStoreData) sSQLText = "INSERT INTO CartProductsOptions (idCartRow, idOption, optionDescrip, priceToAdd, idOptionGroup, TextDetail) VALUES (" & sIDCartRow & "," & aOptions(iX, 1) & ",'" & SQLEncode(pOptionDescrip) & "'," & pPriceToAdd & ", " & aOptions(iX, 0) & ", '" & SQLEncode(aOptions(iX, 3)) & "')" call DB_updateDatabase(sSQLText) Next End If Else ' found a match! increase the qty by one sSQLText = "SELECT Quantity FROM CartProducts WHERE idCartRow = " & sIDCartRow Call DB_getFromDatabase(sSQLText, rsStoreData, "") if not rsStoreData.eof then sQuantity = sQuantity + rsStoreData("Quantity") End If Call DB_closeRecordset(rsStoreData) If ( sQuantity > iStock ) And ( sOWUnderStockBehavior = 0 ) Then Call owMessage(Replace(LANG_DT( "ADDITEM_MAXAMOUNT", "Unfortunately the amount of product you are attempting to add to your cart is more than we have in stock. Please return and choose fewer than 1: to add to your cart." ) ,"1:", iStock), C_ERROR) Else sSQLText = "UPDATE CartProducts SET quantity = " & sQuantity & ", unitPrice = " & replace(sUnitPrice,",",".") & ", unitBToBPrice = " & replace(sUnitBtoBPrice,",",".") & " WHERE idCartRow = " & sIDCartRow call DB_updateDatabase(sSQLText) End If End If SF_ClosePage() response.redirect "owShowCart.asp" End If End If End If End If End If End Sub '************************************************************************************ '************************************************************************************ '* * '* * '* * '************************************************************************************ Function QtySummation() QtySummation = 0 sSQLText = "SELECT Sum(Quantity) FROM CartProducts WHERE idCart = " & sIDCart Call DB_getFromDatabase(sSQLText, rsStoreData, "") If ( NOT rsStoreData.EOF ) Then QtySummation = rsStoreData(0) End If Call DB_closeRecordset(rsStoreData) End Function '************************************************************************************ %>