How do I populate subtotal var using cfloop collection?
I have redesigned a simple shopping cart that uses cfloop collection. What I am trying to do is figure out how to add all prices into a subtotal variable, but I can't get it to work. Below is the basics of what I am using:
<cfloop collection="#session.cart#" item="i">
///////////// definitions
session.cart[1] = prod_id
session.cart[2] = prod_name
session.cart[3] = price
session.cart[4] = Date
session.cart[5] = quantity
//////////// Script to add or delete item
<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 five parts...
// 1. The product id
// 2. The item name
// 3. The price per unit
// 4. The Sale Date
// 5. The quantity
tempvalue = listtoarray('#attributes.id#,#attributes.name#,#attributes.price#,#attributes.date#,#attributes.quantity#');
// if the item is not yet in the cart, simply add it to the cart
if (not(structKeyExists(session.cart, attributes.name))) {
StructInsert(session.cart,attributes.name,tempvalue);
}
// if the item is already in the cart, update the item quantity
else {
tempvalue[5]=session.cart[attributes.name][5]+attributes.quantity;
StructUpdate(session.cart,attributes.name,tempvalue);
}
</cfscript>
//////////// Once quantity has been changed I use the following to change the price
<cfset newPrice = (session.cart[5] * session.cart[3])>
----------------------------------------------------------------------
The cart shows:
| product name (session.cart[2]) | quantity ( session.cart[5]) | newPrice (session.cart[5] * session.cart[3]) |
How do I add up the newPrice column to give me a subtotal?
