<% ' Comersus 4.2x Sophisticated Cart ' Developed by Rodrigo S. Alhadeff ' Dic-2002 ' Open Source License can be found at License.txt ' http://www.comersus.com ' Details: add item to the shopping cart %> <% on error resume next 'Commented out to fix cookie pb 'if readCookie()=0 then response.redirect "cookiesInformation.asp" dim mySQL, connTemp, rsTemp, rsTemp2, pTotalQuantity, lineNumber, pProductHasOptionals, pIdCartRow, pRowPrice, f ' get settings pDefaultLanguage = getSettingKey("pDefaultLanguage") pStoreFrontDemoMode = getSettingKey("pStoreFrontDemoMode") pForceSelectOptionals = getSettingKey("pForceSelectOptionals") pMaxAddCartQuantity = getSettingKey("pMaxAddCartQuantity") pCartQuantityLimit = getSettingKey("pCartQuantityLimit") pStockQuantityControl = getSettingKey("pStockQuantityControl") pTotalQuantity = Cint(0) pIdDbSession = checkSessionData() pIdDbSessionCart = checkDbSessionCartOpen() pIdCustomerType = getSessionVariable("idCustomerType",1) 'added to fix cookie problem pIdStore = getSettingKey("pIdStore") call sessionInit() session("idDbSession") = checkSessionData() session("idStore") = pIdStore 'end added to fix cookie problem if pIdDbSessionCart=0 then pIdDbSessionCart=createNewDbSessionCart() end if ' check for bound quantity in cart if countCartRows(pIdDbSessionCart) = Cint(pCartQuantityLimit) then response.redirect "message.asp?message="&Server.Urlencode(dictLanguage.Item(Session("language")&"_additem_2") ) end if ' get data from viewitem form pIdProduct = getUserInput(request("idProduct"),10) p1StepCheckout = getUserInput(request("1StepCheckout"),3) pOptionsQuantity = getUserInput(request("optionsQuantity"),2) pQuantity = getUserInput(request.form("quantity"),10) pPersonalizationDesc = getUserInput(request.form("personalizationDesc"),150) pIdCustomer = getSessionVariable("idCustomer",0) if pOptionsQuantity="" then pOptionsQuantity=0 end if dim arrOptionGroups(100) ' create an array for options and load idOptionX (idOption1, idOption2...) for each drop down variations for f=0 to pOptionsQuantity-1 arrOptionGroups(f)=getUserInput(request.form("idOption"&(f+1)),4) next ' if cannot get quantity set quantity 1 (means that is adding from listing or search) if pQuantity="" then pQuantity=1 end if ' check if the product has getOneFree mySQL="SELECT idProductFree FROM getFreeProduct WHERE idProduct="&pIdProduct call getFromDatabase (mySql, rsTemp, "addItem") if not rstemp.eof then pIdProductFree =rstemp("idProductFree") else pIdProductFree =0 end if ' check stock (if quantity is higher than stock rejects) mySQL="SELECT stock FROM products WHERE idProduct="&pIdProduct call getFromDatabase(mySQL, rstemp2, "addItem") if not rstemp2.eof then pStock=rstemp2("stock") end if if Cint(pQuantity)>Cint(pMaxAddCartQuantity) then response.redirect "message.asp?message="&Server.Urlencode(dictLanguage.Item(Session("language")&"_additem_6") ) end if ' check how many optionals were assigned mySQL="SELECT COUNT(idOptionGroup) AS howManyOptionals FROM optionsGroups_products WHERE idProduct=" &pIdProduct call getFromDatabase(mySQL, rstemp, "addItem") pHowManyOptionals = 0 if not rstemp.eof then pHowManyOptionals = rstemp("howManyOptionals") end if ' check if there is some Optional selected pHowManyOptionalsSelected=0 for f=0 to pOptionsQuantity-1 if arrOptionGroups(f)<>"" then pHowManyOptionalsSelected=pHowManyOptionalsSelected+1 end if next ' if the product has optionals, is configured to reject and the user has not selected all optionals => send to message if pForceSelectOptionals="-1" and Cint(pHowManyOptionalsSelected)"mysql" then mySQL="SELECT DISTINCT cartRows.idCartRow, quantity, unitPrice, unitBtoBPrice FROM cartRows, cartRowsOptions WHERE cartRowsOptions.idCartRow=cartRows.idCartRow AND cartRows.idDbSessionCart=" &pIdDbSessionCart&" AND cartRows.idProduct=" &pIdProduct& " AND cartRows.idCartRow in ( SELECT idCartRow FROM cartRowsOptions WHERE idOption=" pClosingCounter=0 ' iterate through optionals for f=0 to pOptionsQuantity-1 if arrOptionGroups(f)<>"" then mySql=mySql & arrOptionGroups(f) if arrOptionGroups(f+1)<>"" then ' is not the last mySql =mySql & " AND idCartRow in (SELECT idCartRow FROM cartRowsOptions WHERE idOption=" pClosingCounter =pClosingCounter+1 end if end if next for f=0 to pClosingCounter mySql=mySql&")" next else ' check if the item is in the basket without optionals if lcase(pDataBase)<>"mysql" then mySQL="SELECT cartRows.idCartRow, quantity, unitPrice, unitBtoBPrice FROM cartRows WHERE cartRows.idDbSessionCart=" &pIdDbSessionCart& " AND cartRows.idProduct=" &pIdProduct& " AND cartRows.idCartRow NOT IN (SELECT cartRowsOptions.idCartRow FROM cartRowsOptions WHERE cartRowsOptions.idCartRow=cartRows.idCartRow)" else ' for mySQL use a dummy query (it will allways use a new cart row since mySQL do not allow nested queries) mySQL="SELECT idCartRow FROM cartRows WHERE idCartRow=0" end if end if call getFromDatabase(mySQL, rstemp, "addItem") pIdCartRow=0 if not rstemp.eof then pIdCartRow = rstemp("idCartRow") pOldQuantity = rstemp("quantity") pCartRowPrice = Cdbl(rstemp("unitPrice"))+ Cdbl(rstemp("unitBtoBPrice")) end if if pIdCartRow=0 or pCartRowPrice=0 then ' insert new row line, is not in the cart or the cart row price is 0 (free product) ' check stock and rejects if is not enough for original of get free product if Cint(pQuantity)>Cint(pStock) and pStockQuantityControl="-1" then response.redirect "message.asp?message="&Server.Urlencode(dictLanguage.Item(Session("language")&"_additem_8") ) end if if lcase(pDataBase)="sqlserver" then mySQL="INSERT INTO cartRows (idProduct, quantity, unitPrice, unitBtoBPrice, unitCost, unitWeight, idDbSessionCart, personalizationDesc) VALUES (" &pIdProduct& "," &pQuantity& "," &pUnitPrice& "," &pUnitBtoBPrice& "," &pUnitCost& "," &pUnitWeight& "," &pIdDbSessionCart& ",'" &pPersonalizationDesc& "'); SELECT @@identity" set rstemp=conntemp.execute(mySQL).nextrecordset if err.number <> 0 and err.number<>9 and err.number<>5 and err.number<>13 and err.number<>3265 then response.redirect "supporterror.asp?error="& Server.Urlencode("Error in additem: "&err.description& " SQL:"&mySql) end if pIdCartRow=rstemp(0) end if if lcase(pDataBase)="access" then ' Access set rstemp = Server.CreateObject("ADODB.Recordset") rstemp.Open "cartRows", connTemp, adOpenKeySet, adLockOptimistic, adCmdTable rstemp.AddNew rstemp("idProduct") = pIdProduct rstemp("quantity") = pQuantity rstemp("unitPrice") = pUnitPrice rstemp("unitBtoBPrice") = pUnitBtoBPrice rstemp("unitCost") = pUnitCost rstemp("unitWeight") = pUnitWeight rstemp("idDbSessionCart") = pIdDbSessionCart rstemp("personalizationDesc") = pPersonalizationDesc rstemp.update if err.number <> 0 and err.number<>9 and err.number<>5 and err.number<>13 and err.number<>94 and err.number<>424 and err.number<>3265 and err.number>-2000000000 then response.redirect "supporterror.asp?error="& Server.Urlencode("Error in additem: "&err.description& " - Number:" &Err.Number& " Values:"&pIdProduct&","&pQuantity&","&pUnitPrice&","&pUnitBtoBPrice&","&pUnitCost&","&pUnitWeight&","&pIdDbSessionCart) end if pIdCartRow=rstemp("idCartRow") end if if lcase(pDataBase)="mysql" then mySQL="INSERT INTO cartRows (idProduct, quantity, unitPrice, unitBtoBPrice, unitCost, unitWeight, idDbSessionCart, personalizationDesc) VALUES (" &pIdProduct& "," &pQuantity& "," &pUnitPrice& "," &pUnitBtoBPrice& "," &pUnitCost& "," &pUnitWeight& "," &pIdDbSessionCart& ",'" &pPersonalizationDesc& "')" call updateDatabase(mySQL, rstemp9, "addItem") mySQL ="SELECT idCartRow FROM cartRows WHERE idCartRow=last_insert_id()" call getFromDatabase(mySQL, rstemp9, "addItem") pIdCartRow=rstemp9("idCartRow") end if ' insert optionals for f=0 to pOptionsQuantity-1 if arrOptionGroups(f)<>"" then mySQL="INSERT INTO cartRowsOptions (idCartRow, idOption) VALUES (" &pIdCartRow& "," &arrOptionGroups(f)& ")" call updateDatabase(mySQL, rstemp, "addItem") end if ' <>"" next pNewQuantity = Cint(pQuantity) else ' item is already in the cart pNewQuantity = pOldQuantity + Cint(pQuantity) ' check stock if Cint(pNewQuantity)>Cint(pStock) and pStockQuantityControl="-1" then response.redirect "message.asp?message="&Server.Urlencode(dictLanguage.Item(Session("language")&"_additem_8") ) end if ' check if quantity added + previous quantity is not more than allowed if Cint(pNewQuantity)>Cint(pMaxAddCartQuantity) then response.redirect "message.asp?message="&Server.Urlencode(dictLanguage.Item(Session("language")&"_additem_6")&"*" ) else ' reset unit price before discounts ' replace , by . pUnitPrice = replace(pUnitPrice,",",".") pUnitBtoBPrice = replace(pUnitBToBPrice,",",".") mySQL="UPDATE cartRows SET quantity=" &pNewQuantity& ", unitPrice=" &pUnitPrice& ", unitBToBPrice=" &pUnitBtoBPrice& " WHERE idCartRow=" &pIdCartRow call updateDatabase(mySQL, rstemp, "addItem") end if end if ' load pRowPrice if pIdCustomerType=2 and Cdbl(pUnitBtoBPrice)>0 then pRowPrice=pUnitBtoBPrice else pRowPrice=pUnitPrice end if ' get discount per quantity for old or just inserted line mySQL="SELECT discountPerUnit FROM discountsPerQuantity WHERE idProduct=" &pIdProduct& " AND quantityFrom<=" &pNewQuantity& " AND quantityUntil>=" &pNewQuantity call getFromDatabase(mySQL, rstemp, "addItem") if not rstemp.eof and err.number<>9 then ' there are quantity discounts defined for that quantity pDiscountPerUnit = rstemp("discountPerUnit") pRowPrice = pRowPrice - pDiscountPerUnit ' format for SQL pRowPrice = replace(pRowPrice,",",".") ' update retail or wholesale (unitBtoBPrice) (discounted unit Price) if pIdCustomerType=2 and pUnitBtoBPrice>0 then mySQL="UPDATE cartRows SET unitBtoBPrice=" &pRowPrice& " WHERE idCartRow=" &pIdCartRow else mySQL="UPDATE cartRows SET unitPrice=" &pRowPrice& " WHERE idCartRow=" &pIdCartRow end if call updateDatabase(mySQL, rstemp, "addItem") end if ' for free products use price 0 if getSessionVariable("getFreeProduct",0)=-1 then mySQL="UPDATE cartRows SET unitPrice=0, unitBtoBPrice=0 WHERE idCartRow=" &pIdCartRow call updateDatabase(mySQL, rstemp, "addItem") session("getFreeProduct") =Cint(0) end if call closeDB() call clearLanguage() if pIdProductFree<>0 then ' add the free product with price 0 session("getFreeProduct")=-1 response.redirect "addItem.asp?idProduct="&pIdProductFree end if response.redirect "goToShowCart.asp?1StepCheckout="&p1StepCheckout %>