Create Shopping Cart
| Shopping Cart/E-Commerce Tutorial |
| Using Arrays & Structures -Part 2 of 3- |
| By Michael Bamberger |
| Tutorial Index | |
| # | Section Title |
| 1. | Database Layout |
| 2. | Member Sign Up |
| 3. | *Main Shopping Cart Page |
| 4. | *Product Display - "Add to Cart" |
| 5. | Checkout - Order Processing |
| 6. | Supplemental Coding - Navigation Include |
| Section 3 - Main Shopping Cart Page |
<p>
<table width="90%" align="center" border="0" cellpadding="2" cellspacing="0" style="border:2px
solid #DDDDDD;">
<tr>
<td colspan="7" style="font-weight:bold;font-size:11pt;background-color:#DDDDDD">Items
in Shopping Cart</td>
</tr>
<tr>
<td><b>#</b></td><td><b>Product</b></td><td><b>Size</b></td><td><b>Unit
Price</b></td><td><b>Quantity</b></td><td><b>Total</b></td><td><b>Action</b></td>
</tr>
<cfoutput>
<cfloop from="1" to="#arrayLen(session.shoppingcart)#" index="i">
<cfset total[i] = session.shoppingcart[i].price * session.shoppingcart[i].quantity>
<tr style="background-color:##<cfif i MOD 2>EFEFEF<cfelse>FFFFFF</cfif>;">
<td>#i#</td>
<td>#session.shoppingcart[i].name#</td>
<td>#session.shoppingcart[i].size#</td>
<td>#DollarFormat(session.shoppingcart[i].price)#</td>
<td>#session.shoppingcart[i].quantity#</td>
<td>#DollarFormat(total[i])#</td>
<td><a href="delete_item.cfm?id=#i#">Remove Item</a></td>
</tr>
</cfloop>
</cfoutput>
<cfset totalprice = 0>
<cfloop from="1" to="#arrayLen(session.shoppingcart)#" index="i">
<cfset totalprice = variables.totalprice + total[i]>
</cfloop>
<tr style="background-color:#DDDDDD;">
<td> </td><td> </td><td> </td><td> </td><td> </td><td><b><cfoutput>#DollarFormat(variables.totalprice)#</cfoutput></b></td><td><a
href="checkout.cfm"><b>Checkout</b></a></td>
</tr>
</table>
<p>
You currently have <cfoutput><strong>#variables.totalItems#</strong></cfoutput> <cfif
variables.totalItems EQ 1>item<cfelse>items</cfif> in your cart.<br>
<a href="checkout.cfm"><b>Proceed to Checkout >></b></a><p>
<a href="clear_cart.cfm">Clear Shopping Cart</a>
</cfif>
</cfif>
See, it's not so bad at all. Very little coding. Now to the explanations:
<cfif IsDefined('session.shoppingcart') is "NO">
Your shopping cart is currently empty.
<cfelse>
<cfelse>
<cfset totalItems = 0>
<cfloop from="1" to="#arrayLen(session.shoppingcart)#" index="i">
<cfset totalItems = variables.totalItems + session.shoppingcart[i].quantity>
</cfloop>
<cfif totalItems EQ 0>
Your shopping cart is currently empty.
<cfelse>
<cfoutput>
<cfloop from="1" to="#arrayLen(session.shoppingcart)#" index="i">
<cfset total[i] = session.shoppingcart[i].price * session.shoppingcart[i].quantity>
<tr style="background-color:##<cfif i MOD 2>EFEFEF<cfelse>FFFFFF</cfif>;">
<td>#i#</td>
<td>#session.shoppingcart[i].name#</td>
<td>#session.shoppingcart[i].size#</td>
<td>#DollarFormat(session.shoppingcart[i].price)#</td>
<td>#session.shoppingcart[i].quantity#</td>
<td>#DollarFormat(total[i])#</td>
<td><a href="delete_item.cfm?id=#i#">Remove Item</a></td>
</tr>
</cfloop>
</cfoutput>
<cfset totalprice = 0>
<cfloop from="1" to="#arrayLen(session.shoppingcart)#" index="i">
<cfset totalprice = variables.totalprice + total[i]>
</cfloop>
<tr style="background-color:#DDDDDD;">
<td> </td><td> </td><td> </td><td> </td><td> </td><td><b><cfoutput>#DollarFormat(variables.totalprice)#</cfoutput></b></td><td><a
href="checkout.cfm"><b>Checkout</b></a></td>
Very simple. It uses the ArrayDeleteAt function to remove the item from the cart. It uses the variable passed from the product display page to know which item to remove. Then, we use a quick script to forward the user back to the cart.
You probably also notice the "Clear Cart" link. This is similar to the "Remove Item" link, but clears out the entire cart.
| Section 4 - Product Display - "Add to Cart" |
<table border=0 width="100%" cellpadding="2" cellspacing="0">
<tr>
<td><b>Product</b></td><td><b>Price</b></td><td><b>Quantity</b></td>
</tr>
<cfoutput query="getProds">
<form name="Add#getProds.p_id#" action="addtocart.cfm" method="post">
<tr style="background-color:##<cfif currentrow MOD 2>EFEFEF<cfelse>FFFFFF</cfif>;">
<td>#getProds.p_name#</td>
<td>#DollarFormat(getProds.p_price)#</td>
<td><input type="hidden" name="id" value="#getProds.p_id#">
<input type="hidden" name="name" value="#getProds.p_name#">
<input type="hidden" name="p" value="#getProds.p_price#">
<input type="text" name="qnt" value="1" OnKeyPress="if(((event.keyCode < 48)
|| (event.keyCode > 57))) event.returnValue = false;" maxlength="3"> <input
type="submit" value="Add to Cart">
</td>
</tr>
</form>
</cfoutput>
</table>
<cfif newitem EQ 0>
<cfset temp = arrayAppend(session.shoppingcart, structNew())>
<cfset session.shoppingcart[arrayLen(session.shoppingcart)].productID = #form.id#>
<cfset session.shoppingcart[arrayLen(session.shoppingcart)].name = #form.name#>
<cfset session.shoppingcart[arrayLen(session.shoppingcart)].quantity = #form.qnt#>
<cfset session.shoppingcart[arrayLen(session.shoppingcart)].price = #form.p#>
</cfif>
<script>
self.location="cart.cfm";
</script>
Create Shopping Cart
how do I add more items to the cart... do i add a link to circle back to the product_display.cfm page from the cart.cfm? Right now it only adds the item that a person clicks "Add to Cart" for. The other items can not be added unless a new product_display.cfm is launched. thanks, Dave
Any idea why I am getting this error:
Object of type class java.lang.String cannot be used as an array
The error occurred in
D:\...: line 76
74 :
75 :
checkout_signin.cfm?
shopping cart always shows "Empty" Can you help me my cart always shows that it is empty. thank you
I made a few changes to accomodate some things I need, however when I try and add additional information to populate the cart I keep getting Element Undefined Errors.
Product Display -
----I added the above line----
Cart -
#session.shoppingcart[i].Show_ID#
----I try and reference it on the above line----
#session.shoppingcart[i].name#
#DollarFormat(session.shoppingcart[i].price)#
#session.shoppingcart[i].quantity#
#DollarFormat(total[i])#
Add to Cart