Posted April 15, 2007 at
6:27 PM
Tags:
Ask Ben,
ColdFusion
Someone wanted to create a simple Wish List on their site. This wish list was a single-session "shopping cart" that was not used for checkout. It was used only to keep track of items and to help print out a list that could be brought into a store and given to a store clerk.
For this solution, I have created a ColdFusion component, Cart.cfc, that keeps track of its Items using an internal query. This cart does not have ALL the needed features, but I think it is a pretty good base from which to start. Currently, it only allows you to add items and clear the cart. It does not provide any way to update the quantities directly or delete a given item from the list. However, these can be added with some fairly simple methods that use the Item's PKEY value.
As you can see from the code, there are a few core query columns for the Items query that cannot be altered:
PKEY: The unique ID used for each row which will be used to reference and update a particular row.
NAME: Name of the item in the cart.
SKU: Internal SKU value for inventory.
PRICE: Base cost of the item.
QUANTITY: The number of items that match a particular row of item criteria.
SUB_TOTAL: The cost of the row based on the item price and its quantity.
These are part of the Cart by default. In addition to these columns, you can add custom properties during cart creation (the Init() method call). These properties are added as VARCHAR columns to the internal Items query.
Here is an example of how the Cart.cfc can be used to keep track of items and then to print a list:
Launch code in new window » Download code as text file »
- <cfset objCart = CreateObject( "component", "Cart" ).Init(
- "color, size, sex"
- ) />
-
-
- <cfset objItem = objCart.GetNewItem() />
-
- <cfset objItem.Name = "Girl's Baby Doll T-Shirt" />
- <cfset objItem.Price = 15.95 />
- <cfset objItem.Quantity = 1 />
- <cfset objItem.Color = "Pink" />
- <cfset objItem.Size = "M" />
- <cfset objItem.Sex = "Female" />
-
- <cfset objCart.AddItem( objItem ) />
-
-
- <cfset objItem = objCart.GetNewItem() />
-
- <cfset objItem.Name = "Men's Boxer Briefs" />
- <cfset objItem.Price = 7.95 />
- <cfset objItem.Quantity = 3 />
- <cfset objItem.Color = "Black" />
- <cfset objItem.Size = "M" />
- <cfset objItem.Sex = "Male" />
-
- <cfset objCart.AddItem( objItem ) />
-
-
- <cfset qItem = objCart.GetItems() />
-
-
- <cfloop query="qItem">
-
- <h4>
- #qItem.Name#
- </h4>
-
- <p>
- Price: #DollarFormat( qItem.price )#<br />
- Quantity: #qItem.quantity#<br />
- Sub Total: #DollarFormat( qItem.sub_total )#<br />
- </p>
-
- </cfloop>
-
- <h4>
- Cart Overview:
- </h4>
-
- <p>
- Size: #objCart.GetSize()#<br />
- Total: #DollarFormat( objCart.GetTotal() )#<br />
- </p>
Running the above code, we get the following output:
Girl's Baby Doll T-Shirt
Price: $15.95
Quantity: 1
Sub Total: $15.95
Men's Boxer Briefs
Price: $7.95
Quantity: 3
Sub Total: $23.85
Cart Overview:
Size: 4
Total: $39.80
I am not an eCommerce person. I have not done that much with shopping carts so I am not sure if this is the best way to go. But, it seems like a fairly straight forward way of keeping a Wish List. Here is the code that makes the Cart.cfc possible:
Launch code in new window » Download code as text file »
- <cfcomponent
- output="false">
-
-
- <cfset VARIABLES.InstanceID = CreateUUID() />
-
-
- <cfset VARIABLES.Instance = StructNew() />
-
- <cfset VARIABLES.Instance.Items = QueryNew(
- "pkey, name, sku, price, quantity, sub_total",
- "CF_SQL_INTEGER, CF_SQL_VARCHAR, CF_SQL_VARCHAR, CF_SQL_DECIMAL, CF_SQL_INTEGER, CF_SQL_DECIMAL"
- ) />
-
- <cfset VARIABLES.Instance.CustomProperties = "" />
-
- <cfset VARIABLES.Instance.Size = 0 />
-
- <cfset VARIABLES.Instance.Total = 0 />
-
- <cfset VARIABLES.Instance.NewItem = StructNew() />
-
- <cfset VARIABLES.Instance.NewItem.Name = "" />
- <cfset VARIABLES.Instance.NewItem.SKU = "" />
- <cfset VARIABLES.Instance.NewItem.Price = 0 />
- <cfset VARIABLES.Instance.NewItem.Quantity = 0 />
-
-
- <cffunction
- name="Init"
- access="public"
- returntype="any"
- output="false"
- hint="Returns an initialized Cart instance.">
-
- <cfargument
- name="Properties"
- type="string"
- required="false"
- default=""
- hint="A list of additional properties / columns to add to our internal Items query. Duplicate values will be ignored."
- />
-
-
- <cfset var LOCAL = StructNew() />
-
-
- <cfloop
- index="LOCAL.Property"
- list="#ARGUMENTS.Properties#"
- delimiters=",">
-
- <cfset LOCAL.Property = Trim( LOCAL.Property ) />
-
- <cfif NOT ListFindNoCase(
- VARIABLES.Instance.Items.ColumnList,
- LOCAL.Property
- )>
-
- <cfset QueryAddColumn(
- VARIABLES.Instance.Items,
- LOCAL.Property,
- "CF_SQL_VARCHAR",
- ArrayNew( 1 )
- ) />
-
- <cfset VARIABLES.Instance.CustomProperties = ListAppend(
- VARIABLES.Instance.CustomProperties,
- LOCAL.Property
- ) />
-
-
- <cfset VARIABLES.Instance.NewItem[ LOCAL.Property ] = "" />
-
- </cfif>
-
- </cfloop>
-
-
- <cfreturn THIS />
- </cffunction>
-
-
- <cffunction
- name="AddItem"
- access="public"
- returntype="struct"
- output="false"
- hint="Adds a new item to the Items query. If an item with the matching criteria already exists, the quantity is simply updated.">
-
- <cfargument
- name="Item"
- type="struct"
- required="true"
- hint="The struct generated by a call to GetNewItem()."
- />
-
-
- <cfset var LOCAL = StructNew() />
-
-
- <cflock
- name="#VARIABLES.InstanceID#"
- type="EXCLUSIVE"
- timeout="10">
-
-
- <cfquery name="LOCAL.Exists" dbtype="query">
- SELECT
- pkey
- FROM
- VARIABLES.Instance.Items
- WHERE
- name = <cfqueryparam value="#ARGUMENTS.Item.Name#" cfsqltype="CF_SQL_VARCHAR" />
- AND
- sku = <cfqueryparam value="#ARGUMENTS.Item.SKU#" cfsqltype="CF_SQL_VARCHAR" />
-
- AND
- CAST( price AS DECIMAL ) = <cfqueryparam value="#ARGUMENTS.Item.Price#" cfsqltype="CF_SQL_DECIMAL" />
-
- <cfloop
- index="LOCAL.Property"
- list="#VARIABLES.Instance.CustomProperties#"
- delimiters=",">
-
- AND
- [#LOCAL.Property#] = <cfqueryparam value="#ARGUMENTS.Item[ LOCAL.Property ]#" cfsqltype="CF_SQL_VARCHAR" />
-
- </cfloop>
- </cfquery>
-
-
- <cfif LOCAL.Exists.RecordCount>
-
- <cfset LOCAL.RowIndex = VARIABLES.Instance.Items[ "pkey" ].IndexOf(
- JavaCast( "int", LOCAL.Exists.pkey )
- ) />
-
- <cfset LOCAL.RowIndex = (LOCAL.RowIndex + 1) />
-
- <cfset VARIABLES.Instance.Items[ "quantity" ][ LOCAL.RowIndex ] = (VARIABLES.Instance.Items[ "quantity" ][ LOCAL.RowIndex ] + ARGUMENTS.Item.Quantity) />
-
- <cfset VARIABLES.Instance.Items[ "sub_total" ][ LOCAL.RowIndex ] = (VARIABLES.Instance.Items[ "price" ][ LOCAL.RowIndex ] * VARIABLES.Instance.Items[ "quantity" ][ LOCAL.RowIndex ]) />
-
- <cfset ARGUMENTS.Item.Pkey = LOCAL.Exists.pkey />
-
- <cfelse>
-
- <cfif VARIABLES.Instance.Items.RecordCount>
-
- <cfset LOCAL.Pkey = (
- ArrayMax(
- VARIABLES.Instance.Items[ "pkey" ]
- ) + 1
- ) />
-
- <cfelse>
-
- <cfset LOCAL.Pkey = 1 />
-
- </cfif>
-
-
- <cfset QueryAddRow(
- VARIABLES.Instance.Items
- ) />
-
- <cfset LOCAL.RowIndex = VARIABLES.Instance.Items.RecordCount />
-
- <cfset VARIABLES.Instance.Items[ "pkey" ][ LOCAL.RowIndex ] = JavaCast( "int", LOCAL.Pkey ) />
-
- <cfset VARIABLES.Instance.Items[ "price" ][ LOCAL.RowIndex ] = JavaCast( "float", ARGUMENTS.Item.Price ) />
- <cfset VARIABLES.Instance.Items[ "quantity" ][ LOCAL.RowIndex ] = JavaCast( "int", ARGUMENTS.Item.Quantity ) />
- <cfset VARIABLES.Instance.Items[ "sub_total" ][ LOCAL.RowIndex ] = JavaCast( "float", (ARGUMENTS.Item.Price * ARGUMENTS.Item.Quantity) ) />
-
- <cfloop
- index="LOCAL.Property"
- list="name,sku,#VARIABLES.Instance.CustomProperties#"
- delimiters=",">
-
- <cfset VARIABLES.Instance.Items[ LOCAL.Property ][ LOCAL.RowIndex ] = JavaCast( "string", ARGUMENTS.Item[ LOCAL.Property ] ) />
-
- </cfloop>
-
- <cfset ARGUMENTS.Item.Pkey = LOCAL.Pkey />
-
- </cfif>
-
-
- <cfset VARIABLES.Instance.Size = ArraySum(
- VARIABLES.Instance.Items[ "quantity" ]
- ) />
-
- <cfset VARIABLES.Instance.Total = ArraySum(
- VARIABLES.Instance.Items[ "sub_total" ]
- ) />
-
- </cflock>
-
-
- <cfreturn ARGUMENTS.Item />
- </cffunction>
-
-
- <cffunction
- name="Clear"
- access="public"
- returntype="void"
- output="false"
- hint="Clears the carts.">
-
- <cfquery name="VARIABLES.Instance.Items" dbtype="query">
- SELECT
- *
- FROM
- VARIABLES.Instance.Items
- WHERE
- 1 = 0
- </cfquery>
-
- <cfset VARIABLES.Instance.Size = 0 />
- <cfset VARIABLES.Instance.Total = 0 />
-
- <cfreturn />
- </cffunction>
-
-
- <cffunction
- name="GetItems"
- access="public"
- returntype="query"
- output="false"
- hint="Returns a duplicate of the internal Items query.">
-
- <cfreturn Duplicate( VARIABLES.Instance.Items ) />
- </cffunction>
-
-
- <cffunction
- name="GetNewItem"
- access="public"
- returntype="struct"
- output="false"
- hint="This returns and empty-value struct that can be used to add a new item to the Items query.">
-
- <cfreturn Duplicate(
- VARIABLES.Instance.NewItem
- ) />
- </cffunction>
-
-
- <cffunction
- name="GetSize"
- access="public"
- returntype="numeric"
- output="false"
- hint="Returns the number of items in the cart.">
-
- <cfreturn VARIABLES.Instance.Size />
- </cffunction>
-
-
- <cffunction
- name="GetTotal"
- access="public"
- returntype="numeric"
- output="false"
- hint="Returns the total price of the cart.">
-
- <cfreturn VARIABLES.Instance.Total />
- </cffunction>
-
- </cfcomponent>
Download Code Snippet ZIP File
Comments (0) |
Post Comment |
Ask Ben |
Permalink |
Other Searches |
Print Page
What Other People Are Searching For
[ local search ]
shopping cart
[ local search ]
ecommerce cart
[ local search ]
coldfusion shopping cart
[ local search ]
shopping cart coldfusion
There are no comments posted for this web log entry.
Post Comment |
Ask Ben