My Personal preference is for a session based shopping cart with an option 
to save the cart for the user. That way, if the user wants to leave and 
come back they have that option available. This way you get the best of 
both worlds; a) the speed benefits from not writing to the DB and cleaning 
out old records, b) the permanency of a db driven cart.

I've been using an array based cart (close to Ben Forta's example). It 
might not be the best looking piece of code ever written and I'm not sure 
if the array is being fully utilized for all its worth but this works great:




<!--- act_additem.cfm --->
<!--- //////////////////////// create  the cart (if it doesn't already 
exists ) and add an item ////////////////////////// --->



<cfparam name="id" default="0">
<cfparam name="quantity" default="1">
<cfparam name="title" default="0">
<cfparam name="type" default="0">
<cfparam name="price" default="0">
<cfparam name="weight" default="0">

<cfset x="100">

<!--- if the users shopping cart array is not defined, create it now --->
<cflock timeout="10" scope="SESSION" throwontimeout="Yes" type="exclusive">
         <cfif not isdefined("session.shoppingcart")>
                 <cfset session.shoppingcart = ArrayNew(3)>
         </cfif>
</cflock>

<!--- create a stucture with additional item information. This structure 
will be inserted into the Array --->
<cfset tempStruct=StructNew()>
<cfset tempStruct.title="#title#">
<cfset tempStruct.price="#price#">
<cfset tempStruct.type="#type#">
<cfset tempStruct.weight="#weight#">

<!--- add item to users Cart Array --->
<cflock timeout="10" scope="session" throwontimeout="Yes" type="Exclusive">
         <cfset 
session.shoppingcart[Arraylen(session.shoppingcart)+1][1][1]="#id#">
                 <cfset 
session.shoppingcart[Arraylen(session.shoppingcart)][1][2]="#quantity#">
         <cfset 
session.shoppingcart[Arraylen(session.shoppingcart)][1][3]="#tempStruct#">
</cflock>



<!---- act_deleteitem.cfm --->
<!--- //////////////////////// remove item ////////////////////////// --->

<cfparam name="attributes.id" default="">

<cflock timeout="10" scope="session" throwontimeout="Yes" type="exclusive">
<cfif isdefined("session.shoppingcart")>
         <cfif ArrayLen(session.shoppingcart) is 0>
                 <script>alert('Your shopping cart is already empty')</script>
         <cfelse>
                 <cflock scope="session" type="exclusive" timeout="10">
                         <cfset 
temp=ArraydeleteAT(session.shoppingcart,#attributes.ID#)>
                 </cflock>
         </cfif>
</cfif>
</cflock>






<!---act_update.cfm --->
<!--- //////////////////////// update quantities ////////////////////////// 
--->


<!--- init genral parameters --->
<cfparam name="ID" default="">

<cfif isnumeric(quantity)>

<cfif isdefined("session.shoppingcart")>
         <cfif NOT arrayisempty(session.shoppingcart)>
           <!--- quantity update --->
                 <cfif quantity is "0">
                         <cfinclude template="act_removeitem.cfm"> <!---- 
thats the code snippet above --->
                 <cfelse>
                         <cflock type="exclusive" timeout="10" 
scope="session" throwontimeout="Yes">
                                 <cfset 
session.shoppingcart[ID][1][2]="#quantity#">
                         </cflock>
                 </cfif>
         </cfif>
  </cfif>

          <cfelse>
                 <cfset return_message="The quantity you tried to update 
was not a numeric value. Please try again.">
</cfif>


















At 02:34 PM 21/12/00 -0500, you wrote:
>Nat, I think the red one would look good on you!! he he he
>
>Seriously, what is the general consensus on Shopping carts? I have seen
>session scoped structures used, client structures, even a database query
>driven cart like the one Steve has in the new Jazzloft code.  What is the
>best solution?  I have a site that I have been working on that uses a client
>scoped structure to hold the cart but its the first one I have written so I
>really do not know the "preferred" method.
>
>Thanks,
>Russ
>
>-----Original Message-----
>From: Nat Papovich [mailto:[EMAIL PROTECTED]]
>Sent: Thursday, December 21, 2000 12:54 PM
>To: Fusebox
>Subject: RE: Fusebox Shopping Cart
>
>
>I'm actually working on an implementation of ComUnion right now. The site
>launched yesterday - www.dcprom.com.
>
>Although it can be considered Fusebox, you won't learn the "book-style" of
>Fusebox from the code. It's written in a way that allowed a very complex
>situation to be handled as gracefully as possible.
>
>A version 2.5 (or what the heck, we might just jump to 6.0) will be released
>in the first half of the year...
>
>Nat Papovich
>Webthugs Consulting
>ICQ 32676414
>"If it was hard to write,"
>says the Real Programmer,
>"it should be hard to understand."
>
>
> > -----Original Message-----
> > From: Erik Voldengen [mailto:[EMAIL PROTECTED]]
> > Sent: Thursday, December 21, 2000 8:54 AM
> > To: Fusebox
> > Subject: RE: Fusebox Shopping Cart
> >
> >
> > Look in the list archive for posts from Nat.  He's
> > mentioned a nice one several times.
> >
> >
>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        Structure your ColdFusion code with Fusebox. Get the official book at 
http://www.fusionauthority.com/bkinfo.cfm

Archives: http://www.mail-archive.com/[email protected]/
Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists

Reply via email to