1. <!---
  2.   Filename: ShoppingCart.cfc
  3.   Created by: Nate Weiss (NMW)
  4.   Additions by: Nick Sweeney Big Fat Designs LLC
  5.   Todd Gasall
  6.   Purpose: Creates a CFC called ShoppingCart
  7. --->
  8.  
  9. <CFCOMPONENT>
  10. <!--- Initialize the cart's contents --->
  11. <!--- Because this is outside of any <CFFUNCTION>
  12. tag, --->
  13. <!--- it only occurs when the CFC is first created --->
  14. <CFSET This.CartArray = ArrayNew(1)>
  15.  
  16.  
  17. <!--- *** ADD Method *** --->
  18. <CFFUNCTION
  19. NAME="Add"
  20. HINT="Adds an item to the shopping cart">
  21. <!--- Two Arguments: MerchID and Quantity --->
  22. <CFARGUMENT NAME="MerchID" TYPE="numeric" REQUIRED="Yes">
  23. <CFARGUMENT NAME="Quantity" TYPE="numeric" REQUIRED="No" DEFAULT="1">
  24. <CFARGUMENT NAME="Options" TYPE="struct" REQUIRED="No">
  25. <CFARGUMENT NAME="Choices" TYPE="struct" REQUIRED="No">
  26.  
  27. <!--- Get the Further Info for Cart Item --->
  28. <cfquery name="qryGetCartWeight" datasource="#Application.DSN#" username="#Application.username#" password="#Application.password#">
  29. SELECT MerchID, MerchShipWeight
  30. FROM Merchandise
  31. WHERE MerchID = <cfqueryparam cfsqltype="cf_sql_integer" value="#Arguments.MerchID#">
  32. </cfquery>
  33.  
  34. <!--- Get structure that represents this item in cart, --->
  35. <!--- then set its quantity to the specified quantity --->
  36. <CFSET CartItem = GetCartItem(MerchID,Options,Choices)>
  37. <CFSET CartItem.Quantity = CartItem.Quantity + Arguments.Quantity>
  38. <CFSET CartItem.Weight = #qryGetCartWeight.MerchShipWeight# * CartItem.Quantity>
  39. <CFSET CartItem.Options = #Arguments.Options#>
  40. <CFSET CartItem.Choices = #Arguments.Choices#>
  41.  
  42. </CFFUNCTION>
  43.  
  44.  
  45. <!--- *** UPDATE Method *** --->
  46. <CFFUNCTION
  47. NAME="Update"
  48. HINT="Updates an item's quantity in the shopping cart">
  49. <!--- Two Arguments: MerchID and Quantity --->
  50. <CFARGUMENT NAME="MerchID" TYPE="numeric" REQUIRED="Yes">
  51. <CFARGUMENT NAME="Quantity" TYPE="numeric" REQUIRED="Yes">
  52.  
  53. <!--- If the new quantity is greater than zero --->
  54. <CFIF Quantity GT 0>
  55.  
  56. <!--- Get the Further Info for Cart Item --->
  57. <cfquery name="qryGetCartWeight" datasource="#Application.DSN#" username="#Application.username#" password="#Application.password#">
  58. SELECT MerchID, MerchShipWeight
  59. FROM Merchandise
  60. WHERE MerchID = <cfqueryparam cfsqltype="cf_sql_integer" value="#Arguments.MerchID#">
  61. </cfquery>
  62.  
  63. <!--- Get structure that represents this item in cart, --->
  64. <!--- then set its quantity to the specified quantity --->
  65. <CFSET CartItem = GetCartItem(MerchID)>
  66. <CFSET CartItem.Quantity = Arguments.Quantity>
  67. <CFSET CartItem.Weight = #qryGetCartWeight.MerchShipWeight# * CartItem.Quantity>
  68. <!--- If new quantity is zero, remove the item from cart --->
  69. <CFELSE>
  70. <CFSET This.Remove(MerchID)>
  71. </CFIF>
  72. </CFFUNCTION>
  73.  
  74.  
  75. <!--- *** REMOVE Method *** --->
  76. <CFFUNCTION
  77. NAME="Remove"
  78. HINT="Removes an item from the shopping cart">
  79. <!--- One Argument: MerchID --->
  80. <CFARGUMENT NAME="MerchID" TYPE="numeric" REQUIRED="Yes">
  81.  
  82. <!--- What position is this item occupying in the cart? --->
  83. <CFSET CartPos = GetCartPos(MerchID)>
  84.  
  85. <!--- Assuming the item was found, remove it from cart --->
  86. <CFIF CartPos GT 0>
  87. <CFSET ArrayDeleteAt(This.CartArray, CartPos)>
  88. </CFIF>
  89. </CFFUNCTION>
  90.  
  91.  
  92. <!--- *** EMPTY Method *** --->
  93. <CFFUNCTION
  94. NAME="Empty"
  95. HINT="Removes all items from the shopping cart">
  96.  
  97. <!--- Empty the cart by clearing the This.CartArray array --->
  98. <CFSET ArrayClear(This.CartArray)>
  99. </CFFUNCTION>
  100.  
  101.  
  102. <!--- *** LIST Method *** --->
  103. <CFFUNCTION
  104. NAME="List"
  105. HINT="Returns a query object containing all items in shopping cart. The query object has two columns: MerchID and Quantity."
  106. RETURNTYPE="query">
  107.  
  108. <!--- Create a query, to return to calling process --->
  109. <CFSET q = QueryNew("MerchID,Quantity,Weight,Options,Choices")>
  110.  
  111. <!--- For each item in cart, add row to query --->
  112. <CFLOOP FROM="1" TO="#ArrayLen(This.CartArray)#" INDEX="i">
  113. <CFSET QueryAddRow(q)>
  114. <CFSET QuerySetCell(q, "MerchID", This.CartArray[i].MerchID)>
  115. <CFSET QuerySetCell(q, "Quantity", This.CartArray[i].Quantity)>
  116. <CFSET QuerySetCell(q, "Weight", This.CartArray[i].Weight)>
  117. <CFSET QuerySetCell(q, "Options", This.CartArray[i].Options)>
  118. <CFSET QuerySetCell(q, "Choices", This.CartArray[i].Choices)>
  119. </CFLOOP>
  120.  
  121. <!--- Return completed query --->
  122. <CFRETURN q>
  123. </CFFUNCTION>
  124.  
  125.  
  126. <!--- Internal GetCartPos() Method --->
  127. <CFFUNCTION
  128. NAME="GetCartPos"
  129. RETURNTYPE="numeric"
  130. ACCESS="private">
  131. <!--- Argument: MerchID --->
  132. <CFARGUMENT NAME="MerchID" TYPE="numeric" REQUIRED="Yes">
  133. <CFARGUMENT NAME="Options" TYPE="struct" REQUIRED="No" default="#StructNew()#">
  134. <CFARGUMENT NAME="Choices" TYPE="struct" REQUIRED="No" default="#StructNew()#">
  135.  
  136. <!--- Get position, if any, of MerchID in cart query --->
  137. <CFSET CurrentArrayPos = 0>
  138. <CFLOOP FROM="1" TO="#ArrayLen(This.CartArray)#" INDEX="i">
  139.  
  140. <cfset OptionsMatch = "TRUE">
  141. <cfset ChoicesMatch = "TRUE">
  142.  
  143. <CFIF This.CartArray[i].MerchID EQ Arguments.MerchID>
  144.  
  145.  
  146.  
  147. <!--- Need to compare both Options Struct AND Choice Struct --->
  148. <cfif StructCount(Arguments.Options) GT 0>
  149.  
  150. <cfset CartItemOptions = This.CartArray[i].Options>
  151.  
  152. <!--- Loop over Option Items to find similar value --->
  153. <cfloop collection="#Arguments.Options#" item="o">
  154. <cfset PassedOptionItemID = #StructFind(Arguments.Options, o)# >
  155. <cfset CurrentOptionItemID = #StructFind(CartItemOptions, o)# >
  156.  
  157.  
  158. <!--- If the values match - remove from temp list --->
  159. <cfif PassedOptionItemID EQ CurrentOptionItemID>
  160. <cfset DeleteRow = StructDelete(CartItemOptions, "#o#", "True")>
  161. <cfelse>
  162. <!--- If they don't match - set to FALSE and break loop --->
  163. <cfset OptionsMatch = "FALSE">
  164. <CFBREAK>
  165. </cfif>
  166. </cfloop>
  167.  
  168. </cfif>
  169.  
  170.  
  171. <cfif StructCount(Arguments.Choices) GT 0>
  172.  
  173. <cfset CartItemChoices = This.CartArray[i].Choices>
  174.  
  175. <!--- Loop over Option Items to find similar value --->
  176. <cfloop collection="#Arguments.Choices#" item="c">
  177. <cfset PassedChoiceItemID = #StructFind(Arguments.Choices, c)# >
  178. <cfset CurrentChoiceItemID = #StructFind(CartItemChoices, c)# >
  179.  
  180.  
  181. <!--- If the values match - remove from temp list --->
  182. <cfif PassedChoiceItemID EQ CurrentChoiceItemID>
  183. <cfset DeleteRow = StructDelete(CartItemChoices, "#c#", "True")>
  184. <cfelse>
  185. <!--- If they don't match - set to FALSE and break loop --->
  186. <cfset ChoicesMatch = "FALSE">
  187. <CFBREAK>
  188. </cfif>
  189. </cfloop>
  190.  
  191. </cfif>
  192.  
  193.  
  194.  
  195.  
  196. <!--- Check size of CartItemOptions - if anything left - the two do NOT match--->
  197. <cfif OptionsMatch EQ "TRUE" AND ChoicesMatch EQ "TRUE">
  198. <cfif StructCount(CartItemOptions) EQ 0 AND StructCount(CartItemChoices) EQ 0>
  199. <CFSET CurrentArrayPos = i>
  200. </cfif>
  201. </cfif>
  202. <CFBREAK>
  203.  
  204.  
  205.  
  206.  
  207.  
  208. </CFIF>
  209.  
  210. </CFLOOP>
  211.  
  212. <!--- Return the position --->
  213. <CFRETURN CurrentArrayPos>
  214. </CFFUNCTION>
  215.  
  216.  
  217. <!--- Internal GetCartItem() Method --->
  218. <CFFUNCTION
  219. NAME="GetCartItem"
  220. RETURNTYPE="struct"
  221. ACCESS="private">
  222. <!--- One Argument: MerchID --->
  223. <CFARGUMENT NAME="MerchID" TYPE="numeric" REQUIRED="Yes">
  224. <CFARGUMENT NAME="Options" TYPE="struct" REQUIRED="no">
  225. <CFARGUMENT NAME="Choices" TYPE="struct" REQUIRED="no">
  226.  
  227. <!--- Get the position of the item in This.CartArray --->
  228. <CFSET CartPos = GetCartPos(MerchID,Options,Choices)>
  229.  
  230. <!--- If item for this MerchID was found, we will return it --->
  231. <CFIF CartPos GT 0>
  232. <CFSET CartItem = This.CartArray[CartPos]>
  233. <!--- If item was not found, create new one and add to cart --->
  234. <CFELSE>
  235. <CFSET CartItem = StructNew()>
  236. <CFSET CartItem.MerchID = Arguments.MerchID>
  237. <CFSET CartItem.Quantity = 0>
  238. <CFSET CartItem.Weight = 0>
  239. <CFSET CartItem.Options = StructNew()>
  240. <CFSET CartItem.Choices = StructNew()>
  241.  
  242. <CFSET ArrayAppend(This.CartArray, CartItem)>
  243. </CFIF>
  244.  
  245. <!--- In either case, return the item --->
  246. <CFRETURN CartItem>
  247. </CFFUNCTION>
  248.  
  249. </CFCOMPONENT>