Copy link to clipboard
Copied
In my array for a Shopping Cart, the customer can select to add, subtract or delete an item.
All the functions work.
In the code to subtract, when the function is invoked, quantity changes from 1 to 0 in the cart, with a total value of $0.00.
A line also appears telling the customer that the cart is empty.
But if you click the subtract one more time, quantity changes from 0 to -1, with a value of -$5.00.
How can I delete an item when the subtraction results in a quantity of 0?
Many thanks, Charlie
CART.CFM
<cfif isdefined("url.action")>
<cfif url.action is "addQuantity">
<cfset session.cart.addQuantity(url.position)>
<cflocation url="cart_main.cfm">
</cfif>
<cfif url.action is "subtractQuantity">
<cfset session.cart.subtractQuantity(url.position)>
<cflocation url="cart_main.cfm">
</cfif>
<cfif url.action is "delete">
<cfset session.cart.removeItem(url.position)>
<cflocation url="cart_main.cfm">
</cfif>
</cfif>
<cfset arCart = session.cart.getCart()>
<table>
<cfoutput>
<cfloop from="1" to="#arraylen(arCart)#" index="counter">
<tr valign="top">
<td>#arCart[counter].isd_code#</td>
<td></td>
<td>#arCart[counter].title.gettitle()#</td>
<td></td>
<td>#dollarformat(arCart[counter].price)#</td>
<td></td>
<td>#arCart[counter].ItemName#</td>
<td></td>
<td align="center">#arCart[counter].quantity#</td>
<td></td>
<td><a href="cart.cfm?action=addQuantity&position=#counter#">ADD</a></td>
<td></td>
<td><a href="cart.cfm?action=subtractQuantity&position=#counter#">SUB</a></td>
<td></td>
<td><a href="cart.cfm?action=delete&position=#counter#">DEL</a></td>
<td></td>
<td>
<cfset variable.itemTotal = (#arCart[counter].quantity# * #arCart[counter].price#)>
#dollarFormat(variable.itemTotal)#
</td>
</tr>
</cfloop>
</cfoutput>
</table>
SHOPPINGCART.CFC
<cfcomponent output="false">
<cfproperty name="arCart" type="array" default="arraynew(1)" />
<cfscript>
variables.arCart = arraynew(1);
</cfscript>
<cffunction name="subtractQuantity" access="public" output="false" returntype="void">
<cfargument name="position" required="true" type="numeric">
<cfparam name="subQty" type="numeric" default="1">
<cfset arCart[position].quantity = arCart[position].quantity - subQty>
</cffunction>
<cffunction name="removeItem" access="public" output="false" returntype="void">
<cfargument name="position" required="true" type="numeric">
<cfset arraydeleteat(arCart, arguments.position)>
</cffunction>
</cfcomponent>
It's hard to understand exactly what's going there but this code might help set you in the right direction. It basically looks at the quantity of the item for the given position and it won't let you subtract more than the number of items that are in it, or go below zero.
...
<cffunction name="subtractQuantity" access="public" output="false" returntype="void">
<cfargument name="position" required="true" type="numeric">
<cfargument name="subQty" type="numeric" default="1">
<cfif arCart[arg
Copy link to clipboard
Copied
It's hard to understand exactly what's going there but this code might help set you in the right direction. It basically looks at the quantity of the item for the given position and it won't let you subtract more than the number of items that are in it, or go below zero.
<cffunction name="subtractQuantity" access="public" output="false" returntype="void">
<cfargument name="position" required="true" type="numeric">
<cfargument name="subQty" type="numeric" default="1">
<cfif arCart[arguments.position].quantity gte 1 and arguments.subQty lte arCart[arguments.position].quantity>
<cfset arCart[arguments.position].quantity = (arCart[arguments.position].quantity - arguments.subQty)>
<cfelse>
<cfset arCart[arguments.position].quantity = 0 >
</cfif>
</cffunction>
One other thing I noticed in your subtractQuantity() function was that you have a cfparam instead of a cfargument tag, this is probably also causing you issues.
-JD
Copy link to clipboard
Copied
Greetings JD,
Hole filled; problem solved. One line, one cfif statement, changed
everything.
Heartiest thanks,
Charlie