Skip to main content
Inspiring
May 13, 2016
Question

Extremely Simple Shopping Cart

  • May 13, 2016
  • 1 reply
  • 3859 views

Hi everyone. I was able to get a very simple shopping cart set up and to work with adding a Product to the cart and deleting the Product, but how do I add or display something such as the price of the product too? Here's what I have below on a products.cfm page and a ShoppingCart.cfm page:

products.cfm

<form action="ShoppingCart.cfm" method="post">

Product 1 Description

<input type="hidden" name="productID" value="1" />

<input type="hidden" name="productName" value="Socket" />

<input type="submit" value="Add To Cart" />

</form>

<form action="ShoppingCart.cfm" method="post">

Product 2 Description

<input type="hidden" name="productID" value="2" />

<input type="hidden" name="productName" value="Adapter" />

<input type="submit" value="Add To Cart" />

</form>

ShoppingCart.cfm

<cfif NOT StructKeyExists(session,'ShoppingCart')>

  <cfset Session.ShoppingCart = StructNew()>

    <cfset Session.ShoppingCart.FirstName = "">

    <cfset Session.ShoppingCart.LastName = "">

    <cfset Session.ShoppingCart.Address = "">

    <cfset Session.ShoppingCart.Products = ArrayNew(2)>

</cfif>

<cfif StructKeyExists(form,'productID')>

  <cfset Session.ShoppingCart.Products[arraylen(Session.ShoppingCart.Products)+1][1] = form.productID>

    <cfset Session.ShoppingCart.Products[arraylen(Session.ShoppingCart.Products)][2] = form.productName>

</cfif>

<cfif StructKeyExists(url,'deleteID')>

  <cfset variables.products = ArrayNew(2)>

    <cfloop index="i" from="1" to="#arrayLen(Session.ShoppingCart.Products)#">

    <cfif url.deleteID NEQ i>

  <cfset variables.products[arraylen(variables.products)+1][1] = Session.ShoppingCart.Products[1]>

            <cfset variables.products[arraylen(variables.products)][2] = Session.ShoppingCart.Products[2]>

        </cfif>

    </cfloop>

    <cfset Session.ShoppingCart.Products = variables.products>

</cfif>

<cfif StructKeyExists(url,'editID')>

  <cfset Session.ShoppingCart.Products[form.editID][1] = form.productID>

    <cfset Session.ShoppingCart.Products[form.editID][2] = form.productName>

</cfif>

<cfoutput>

You have #arrayLen(Session.ShoppingCart.Products)# items in your cart.<br /><br />

<cfloop index="i" from="1" to="#arrayLen(Session.ShoppingCart.Products)#">

<table>

<tr>

<td>#Session.ShoppingCart.Products[2]#</td>

<td><a href="ShoppingCart.cfm?deleteID=#i#">Delete</a></td>

</tr>

</table>

</cfloop>

</cfoutput>

<br />

<form action="shopping.cfm" method="POST">

  <div class="form-row">

    <label>

      <span>First Name</span>

      <input type="text" size="20" name="FirstName">

    </label>

  </div>

 

    <div class="form-row">

    <label>

      <span>Last Name</span>

      <input type="text" size="20" name="LastName">

    </label>

  </div>

 

    <div class="form-row">

    <label>

      <span>Address</span>

      <input type="text" size="20" name="Address">

    </label>

  </div>

  <input type="submit" class="submit" value="Check Out">

</form>

Thanks for your help.

Andy

This topic has been closed for replies.

1 reply

BKBK
Community Expert
May 14, 2016

jamie61880 wrote:

I was able to get a very simple shopping cart set up and to work with adding a Product to the cart and deleting the Product, but how do I add or display something such as the price of the product too?

The field names, productID and productName, tell you everything you need to know. Wherever there is code about productID and productName you should just add equivalent code about productPrice.

Hence,

<input type="hidden" name="productPrice">

<cfset Session.ShoppingCart.Products[arraylen(Session.ShoppingCart.Products)][3] = form.productPrice>

and so on. Remember to display the total price.

Inspiring
May 16, 2016

BKBK,

    How do I display the price? I have the code below that outputs the ProductName with the [2], but the price doesn't output. I added the Price with [3] like you did above, but if I put this into the code below, I receive a 500 internal server error, that says:

"Error","ajp-bio-8014-exec-11","05/16/16","09:34:04","MySite","You have attempted to dereference a scalar variable of type class java.lang.String as a structure with members. The specific sequence of files included or processed is: D:\websites\Ironwoodelectronics\Sessions\Ben\ShoppingCart.cfm, line: 46 "

coldfusion.runtime.ScopeCastException: You have attempted to dereference a scalar variable of type class java.lang.String as a structure with members.

What does this error mean?

<cfoutput>

You have #arrayLen(Session.ShoppingCart.Products)# items in your cart.<br /><br />

<cfloop index="i" from="1" to="#arrayLen(Session.ShoppingCart.Products)#">

<table>

<tr>

<td>#Session.ShoppingCart.Products[2][3]#</td>

<td><a href="ShoppingCart.cfm?deleteID=#i#">Delete</a></td>

</tr>

</table>

</cfloop>

</cfoutput>

Andy

Inspiring
May 17, 2016

jamie61880 wrote:

How do you set up an Edit box for Quantity? I have this that puts the Quantity into a text box, but how do you update it so it updates the number the user could type in?:

<td><cfinput type="text" name="#Session.ShoppingCart.Products[3]#" value="#Session.ShoppingCart.Products[3]#" passThrough = "readonly = 'True'"></td>

Just remove the readOnly attribute. If you know beforehand that the quanties will be within a small, specific range, then you could use a select-field for the Quantity.


BKBK,

    Just removing the readonly attribute doesn't do anything. It doesn't update the text box after I change the quantity and click the Submit button. I did find this:

<cfif isDefined("Form.submit")>

<td>Quantity: <cfinput type="text" name="productQuantity" value="#productQuantity#"></td>   

<cfelse>

<td><cfinput type="text" name="productQuantity" value="#Session.ShoppingCart.Products[3]#"></td>

</cfif>

One problem with this is after I click the Submit button when I have 12 items in the cart, this gets displayed in the text box for each line item: 1,1,1,1,1,1,1,1,1,9,1,1.   9 is the one I updated. How do I get this narrowed down to only having the one text box for the item I updated, display correctly. So that 9 is updated on Item 10 and the 1's are updated on all the other text boxes? Here's what I have for this whole output of code:

<cfoutput>

<cfform name="ShoppingCart" action="ShoppingCart.cfm" method="post">

You have #arrayLen(Session.ShoppingCart.Products)# items in your cart.<br /><br />

<cfloop index="i" from="1" to="#arrayLen(Session.ShoppingCart.Products)#">

<table>

<tr>

<td>#Session.ShoppingCart.Products[2]#</td>

<cfif isDefined("Form.submit")>

<td>Quantity: <cfinput type="text" name="productQuantity" value="#productQuantity#"></td>   

<cfelse>

<td><cfinput type="text" name="productQuantity" value="#Session.ShoppingCart.Products[3]#"></td>

</cfif>

<td><a href="ShoppingCart.cfm?deleteID=#i#">Delete</a></td>

<td><input type="submit" name="submit" value="submit"></td>

</tr>

</table>

</cfloop>

</cfform>

</cfoutput>

Someone gave me this code for editing a shopping cart, but didn't show me how to use it. Do you know how to use this section otherwise?:

<cfif StructKeyExists(url,'editID')>

  <cfset Session.ShoppingCart.Products[form.editID][1] = form.productID>

    <cfset Session.ShoppingCart.Products[form.editID][2] = form.productName>

    <cfset Session.ShoppingCart.Products[form.editID][3] = form.productQuantity>

</cfif>

Thanks.

Andy