Hi all - here is the puzzle:
My "add to cart button" points to this custom tag code:
<cfparam name="formVal" default="Add to Cart">
<cfif isdefined("form.submit"
>
<cfif form.submit eq FormVal>
<cf_addItem
strProdID="#form.addID#"
strProdName="#form.addName#"
curPrice="#form.addPrice#"
quantity="#form.addQty#">
</cfif>
</cfif>
<cfoutput>
<center>
<FORM action="#getfilefrompath(cgi.script_name)#?first=#FirstRow#&searchcatagory=#searchcatagory#&textsearch=#textsearch#&querycount=#querycount#&strProdID=#strProdID#" method="post">
<input type="hidden" name="addName" value="#strprodName#">
<input type="hidden" name="addID" value="#strProdID#">
<input type="hidden" name="addPrice" value="#curPrice#">
<input type="hidden" name="addQty" value="1">
<input type="submit" name="submit" value="#formVal#">
</form>
</center>
</cfoutput>
Notice that the value for the "addQty" field is "1" (the rest of the values are being filled in correctly by the variables). Now - if you submit the above form, it submits to itself and the code at the top (in the cfif tag) goes into effect and the values are passed to the cf_addItem custom tag (addQty should still be 1, right?). That tag contains the following (sorry about the long post but i cant zero in any further):
<!--- Make sure that all of the necessary attributes are passed in --->
<cfif isdefined("attributes.strProdID"
and isdefined("attributes.strProdName"
and isdefined("attributes.curPrice"
and isdefined("attributes.quantity"
>
<cfscript>
if (not(isdefined("session.cart"
)) {
// Check to make sure that the Shopping cart structure exists.
session.cart = structnew();
}
// The item structure we are going to use to store the items in the cart
// is going to have four parts...
// 1. The item id
// 2. The item name
// 3. The price per unit
// 4. The quantity
tempvalue = listtoarray('#attributes.strProdID#,#attributes.strProdName#,#attributes.curPrice#,#attributes.quantity#');
// if the item is not yet in the cart, simply add it to the cart
if (not(structKeyExists(session.cart, attributes.strProdName))) {
StructInsert(session.cart,attributes.strProdname,tempvalue);
}
// if the item is already in the cart, update the item quantity
else {
tempvalue[4]=session.cart[attributes.strProdname][4]+attributes.quantity;
StructUpdate(session.cart,attributes.strProdName,tempvalue);
}
</cfscript>
</cfif>
WHEW! Okay now, the only part of that i dont really get is the "listtoarray" thing so maybe that is wheer the problem is but when you go to view the contents of the shopping cart, the quantity is 10 not 1! Can anyone figure this out?
(just for fun, here is the relevant code for viewing the cart):
<!--- Make sure that the shopping cart structure exists and is the correct format --->
<cfif isdefined("session.cart"
>
<cfif IsStruct(session.cart)>
<!--- Initialize variables --->
<cfset subtotal = 0>
<cfoutput>
<cfloop collection="#session.cart#" item="i">
<cfset subtotal = subtotal + (session.cart[4] * session.cart[3])>
<tr>
<td valign=top><font face="arial" size=2><a href="productpage.cfm?strProdID=#session.cart[1]#">#session.cart[2]#</a></font></td>
<td valign=top>
<table border=0 width=100%>
<tr>
<td><font face="arial" size=3>#session.cart[4]#</font></td>
<form method="post">
<input type="hidden" name="name" value="#session.cart[2]#">
<input type="hidden" name="id" value="#session.cart[1]#">
<input type="hidden" name="price" value="#session.cart[3]#">
<input type="hidden" name="quantity" value="1">
<td align=right>
<input type="submit" name="action" value=" + "> <input type="submit" name="action" value=" - "></td>
</form>
</tr> </table>
</td>
My "add to cart button" points to this custom tag code:
<cfparam name="formVal" default="Add to Cart">
<cfif isdefined("form.submit"

<cfif form.submit eq FormVal>
<cf_addItem
strProdID="#form.addID#"
strProdName="#form.addName#"
curPrice="#form.addPrice#"
quantity="#form.addQty#">
</cfif>
</cfif>
<cfoutput>
<center>
<FORM action="#getfilefrompath(cgi.script_name)#?first=#FirstRow#&searchcatagory=#searchcatagory#&textsearch=#textsearch#&querycount=#querycount#&strProdID=#strProdID#" method="post">
<input type="hidden" name="addName" value="#strprodName#">
<input type="hidden" name="addID" value="#strProdID#">
<input type="hidden" name="addPrice" value="#curPrice#">
<input type="hidden" name="addQty" value="1">
<input type="submit" name="submit" value="#formVal#">
</form>
</center>
</cfoutput>
Notice that the value for the "addQty" field is "1" (the rest of the values are being filled in correctly by the variables). Now - if you submit the above form, it submits to itself and the code at the top (in the cfif tag) goes into effect and the values are passed to the cf_addItem custom tag (addQty should still be 1, right?). That tag contains the following (sorry about the long post but i cant zero in any further):
<!--- Make sure that all of the necessary attributes are passed in --->
<cfif isdefined("attributes.strProdID"




<cfscript>
if (not(isdefined("session.cart"

// Check to make sure that the Shopping cart structure exists.
session.cart = structnew();
}
// The item structure we are going to use to store the items in the cart
// is going to have four parts...
// 1. The item id
// 2. The item name
// 3. The price per unit
// 4. The quantity
tempvalue = listtoarray('#attributes.strProdID#,#attributes.strProdName#,#attributes.curPrice#,#attributes.quantity#');
// if the item is not yet in the cart, simply add it to the cart
if (not(structKeyExists(session.cart, attributes.strProdName))) {
StructInsert(session.cart,attributes.strProdname,tempvalue);
}
// if the item is already in the cart, update the item quantity
else {
tempvalue[4]=session.cart[attributes.strProdname][4]+attributes.quantity;
StructUpdate(session.cart,attributes.strProdName,tempvalue);
}
</cfscript>
</cfif>
WHEW! Okay now, the only part of that i dont really get is the "listtoarray" thing so maybe that is wheer the problem is but when you go to view the contents of the shopping cart, the quantity is 10 not 1! Can anyone figure this out?
(just for fun, here is the relevant code for viewing the cart):
<!--- Make sure that the shopping cart structure exists and is the correct format --->
<cfif isdefined("session.cart"

<cfif IsStruct(session.cart)>
<!--- Initialize variables --->
<cfset subtotal = 0>
<cfoutput>
<cfloop collection="#session.cart#" item="i">
<cfset subtotal = subtotal + (session.cart[4] * session.cart[3])>
<tr>
<td valign=top><font face="arial" size=2><a href="productpage.cfm?strProdID=#session.cart[1]#">#session.cart[2]#</a></font></td>
<td valign=top>
<table border=0 width=100%>
<tr>
<td><font face="arial" size=3>#session.cart[4]#</font></td>
<form method="post">
<input type="hidden" name="name" value="#session.cart[2]#">
<input type="hidden" name="id" value="#session.cart[1]#">
<input type="hidden" name="price" value="#session.cart[3]#">
<input type="hidden" name="quantity" value="1">
<td align=right>
<input type="submit" name="action" value=" + "> <input type="submit" name="action" value=" - "></td>
</form>
</tr> </table>
</td>