Shopping Cart

I am trying to implement a simply shopping cart. I copied the code below from a Tech Republic's tutorial on creating a shopping cart. I have replace the variable names with those that I am using in my application.  When I run the code I get the error message listed below.

Can you please tell me where I am going wrong, why I am getting the error message and how to resolve the problem.

Thanks in advance



The error message:
Object of type class java.lang.String cannot be used as an array
 
The error occurred in c:\shopping_cart.cfm: line 26

24 : <!--- Append a new element to the array. This element is a new structure. --->
25 : <cfcase value="1">
26 : <cfset session.cart = arrayAppend(session.cart, structNew())>
27 : <!--- Set up a variable to hold the array position that we're inserting into. --->
28 : <cfset thisCartItem = arraylen(session.cart)>

<cfoutput>
<cfparam name="session.cart" default="arrayNew()>">
<cfif isdefined('url.do')>
<cfset ImageID = "#url.ImageID#">
<cfset ImageID = "#url.Title#">
<cfset Price = "#url.Price#">
<cfset Size = "#url.Size#">
<cfset Quantity = "#url.Quantity#">
 
<cfswitch expression="#url.do#">
<!--- Append a new element to the array. This element is a new structure. --->
<cfcase value="1">
<cfset session.cart = arrayAppend(session.cart, structNew())> 
<!--- Set up a variable to hold the array position that we're inserting into. ---> 
<cfset thisCartItem = arraylen(session.cart)> 
<!--- Populate the new structure with the item information passed from the url/form. ---> 
<cfset session.cart[thisCartItem].ImageID = #ImageID#> 
<cfset session.cart[thisCartItem].Title = #Title#>
<cfset session.cart[thisCartItem].Price = #Price#> 
<cfset session.cart[thisCartItem].Size = #Size#>
<cfset session.cart[thisCartItem].Quantity = #Quantity#>
</cfcase>

Open in new window

wsessomsAsked:
Who is Participating?
 
clickitysplitConnect With a Mentor Commented:
sorry:  <cfset arrayAppend(session.cart, structNew())>
0
 
Peter HartCommented:
have you tried running it in its original form?
0
 
wsessomsAuthor Commented:
No , I will try that and let you know.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
wsessomsAuthor Commented:
I tried the code without any changes and got the same error. Below is the code without any changes. I am also attaching the PDF file that I downloaded for their website that contains the code and tutorial I was following.

I must be missing something.
<cfparam name="session.cart" default="arrayNew()">
 
<cfif isdefined('form.act')>
<cfif form.act is "add">
<!--- Append a new element to the array. This element is a new structure. ---> 
<cfset session.cart = arrayAppend( session.cart, structNew() )> 
<!--- Set up a variable to hold the array position that we're inserting into. ---> 
<cfset thisCartItem = arraylen( session.cart )> 
<!--- Populate the new structure with the item information passed from the form. ---> 
<cfset session.cart[thisCartItem].itemID = form.itemID> 
<cfset session.cart[thisCartItem].quantity = form.quantity> 
<cfset session.cart[thisCartItem].itemName = form.itemName>
</cfif>
 
<cfif form.act is "delete">
<!--- Delete the specified item from the cart. ---> 
<cfloop index="thisCartItem" from="1" to="#arrayLen( session.cart )#"> 
<cfif session.cart[thisCartItem].itemID eq form.itemID> 
<cfset session.cart = arrayDeleteAt( session.cart, thisCartItem )> 
</cfif> 
</cfloop>
</cfif>
 
<cfif form.act is "update">
<!--- Update the specified item from the cart. ---> 
<cfloop index="thisCartItem" from="1" to="#arrayLen( session.cart )#"> 
<cfif session.cart[thisCartItem].itemID eq form.itemID> 
<cfset session.cart[thisCartItem].quantity = form.quantity> 
</cfif> 
</cfloop>
</cfif>
 
Done!
</cfif>
 
<form action="testcart.cfm" method="post">
<input type="hidden" name="itemID" value="300" />
<input type="hidden" name="quantity" value="1" />
<input type="hidden" name="itemName" value="Countryside" />
<input type="hidden" name="act" value="add" />
<input type="submit" name="Submit" value="Add to Cart" />
</form>
 
Error Message
 Object of type class java.lang.String cannot be used as an array
 
The error occurred in C:\wwwroot\testcart.cfm: line 14
 
12 : <cfif form.act is "add">
13 : <!--- Append a new element to the array. This element is a new structure. ---> 
14 : <cfset session.cart = arrayAppend( session.cart, structNew() )> 
15 : <!--- Set up a variable to hold the array position that we're inserting into. ---> 
16 : <cfset thisCartItem = arraylen( session.cart )> 

Open in new window

ColdFusionCart.pdf
0
 
wsessomsAuthor Commented:
Its been awhile since I posted this question. I could really use some help fiquring out what the error means maybe.
0
 
clickitysplitCommented:
Try changing:

<cfset session.cart = arrayAppend(session.cart, structNew())>

to:

<cfset arrayAppend(session.cart, structNew()>
0
 
wsessomsAuthor Commented:
Shouldn't there be a variable in <cfset>
ex. <cfset variable_name = arrayAppend(session.cart, structNew())>
0
 
wsessomsAuthor Commented:
clickitysplit:

I tried the changing the code as you suggested and I still got the same error.
I changed the code to this (see Code Snippet) and it adds the content to the basket but it doesn't append to the array when I try to add another item to the basket...it replaces whats already in the array.

<cfset session.cart = ArrayNew(1)>
<cfset thisitem = StructNew()>

What I think is happening is every time the code runs it's creating a new array and adding the content to the first position instead of appending to the array.

How do you check to see if an array exist and by pass it if it does exist and run the rest of the code?

<!--- Append a new element to the array. This element is a new structure. --->
<cfcase value="1">
<cfoutput>
 
<cfset session.cart = ArrayNew(1)>
<cfset thisitem = StructNew()>
 
#ArrayAppend(session.cart, thisitem)#
 
<!--- Set up a variable to hold the array position that we're inserting into. ---> 
<cfset thisCartItem = #arraylen(session.cart)#> 
<!--- Populate the new structure with the item information passed from the url/form. ---> 
<cfset session.cart[thisCartItem].ImageID = #ImageID#> 
<cfset session.cart[thisCartItem].Title = #Title#>
<cfset session.cart[thisCartItem].Price = #Price#> 
<cfset session.cart[thisCartItem].Size = #Size#>
<cfset session.cart[thisCartItem].Quantity = #Quantity#>
</cfoutput>
</cfcase>

Open in new window

0
 
Peter HartCommented:
you are correct - you should put that code in an initalisation procedure call that at the beginning of the buyers  session.
0
 
wsessomsAuthor Commented:
chilternPC
Can you give me an example.
0
 
wsessomsAuthor Commented:
Should I put this into onRequestStart?
0
 
wsessomsAuthor Commented:
Up until this point I have been using the Application.cfm page to initialize variables. I know that there is an Application.cfc page that seems to be what is recommended to be used. I am not sure of the difference and/or how to setup an Application.cfc page. Could someone help me to understand setting initailization variables using the Application.cfc page. Do I have to use <cfcomponent>tags? Please give I get more than a one line answer. I really need help with what really seems to me to be a simple problem. I may be wrong but it seems that way to me.

0
 
Peter HartCommented:
it sounds like you are designing your ecommerce on the fly. do you have an area where the basket is initialised and the session is setup? and also when the session ends (i.e. the shopped may put stuff in the basket but leave the site without buying? does the software handle aborted shopping?

  - for ecommerce I would recommend buying or using tried and tested software (many are free) see
http://www.devscripts.com/category/browse/215/ColdFusion-E-Commerce.html

0
 
wsessomsAuthor Commented:
What I have done is created a photography website. I want to implement a simply shopping cart that keeps track of the items being selected for purchase. I have implemented the code to do checkout using PayPal. PayPal is were I am going to keep track of the purchases. I have already setup a mySQL database for image information and file upload to store the images in folders on the server. I don't want a full blown ecommerce package or I would use CFWebstore which I have used in the past.

I am trying to implement a simply solution.

I am going to implement the Application.cfc page to handle onApplicationStart and onSessionStart variables. I think you have helped me in advising me to initialize the variables in the onSessionStart function without actually saying do that. So I have to go to work now. I will keep this thread update and post my progress.  If you can think of a simple solution to accomplish gather the items selected into a basket please post it.

Thanks
0
 
Peter HartCommented:
oh -  I see - I'll have a think about the interface - have you checked out PayPal's development centre?
https://www.paypal.com/uk/cgi-bin/webscr?cmd=_resource-center
there may be a solution you can use/reuse
0
 
wsessomsAuthor Commented:
Yes I have checked out PayPal's development centre and have all of the information to interface to their checkout process. If I send one item at a time then the code that I have would do the trick but I want to be able to gather more than one item into a shopping basket and then send to PayPal. The PayPal end is not a problem it is getting the shopping cart to hold more than one item.
0
 
Peter HartCommented:
Hmm, I've used Paypal before this way  and simply put their code on each product and paypal kept the basket contents.
sorry can't really help much more - except explain how mine worked - the pages that displayed the product also had paypal code added to allow adding to baskets and checking out. - the variables used to display the product were also then used to put in the paypal code.
I assume you have a page that displays the item that is for sale ?
0
 
wsessomsAuthor Commented:
I want the code to be dynamic. I don't want to cut & paste the code for the add to cart button  each product. That is why the original code that I submitted was aimed at appending the items to the shopping cart. Thanks for your suggestion.
0
 
wsessomsAuthor Commented:
That line of code helped but the thing that actually pulled the script together required putting the code below on the add_to_cart.cfm page.





After adding that code everything worked. Thanks for your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.