Skip to main content
Inspiring
May 13, 2016
Question

Extremely Simple Shopping Cart

  • May 13, 2016
  • 1 reply
  • 3884 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
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

BKBK
Community Expert
Community Expert
May 16, 2016

Which one is line 46?

In any case, you are attempting to use a three-dimensional array,

(Session.ShoppingCart.Products[2][3]),

whereas you had defined 2-D ones,

Session.ShoppingCart.Products[arraylen(Session.ShoppingCart.Products)][1],

Session.ShoppingCart.Products[arraylen(Session.ShoppingCart.Products)][2],

Session.ShoppingCart.Products[arraylen(Session.ShoppingCart.Products)][3],

etc.

I think the third one is the price.