Array removeItem: QTY changes from 1 to 0 to -1; $5.00 to $0.00 to -$5.00
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>
