Copy link to clipboard
Copied
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?
It looks like your newprice var is being used to report on the cost of the line item in your cart (e.g. cost * units). You have a few ways to tracking the subtotal for all items:
1) If you are not displaying the value of "newprice" in your interface, you could use a += operator to add the value of each entry in your array when you assign it:
<cfset newPrice += (session.cart[5] * session.cart[3])>
2) You could create a variable to hold the totalPrice that you add your newPrice value to for each ite
...Copy link to clipboard
Copied
It looks like your newprice var is being used to report on the cost of the line item in your cart (e.g. cost * units). You have a few ways to tracking the subtotal for all items:
1) If you are not displaying the value of "newprice" in your interface, you could use a += operator to add the value of each entry in your array when you assign it:
<cfset newPrice += (session.cart[5] * session.cart[3])>
2) You could create a variable to hold the totalPrice that you add your newPrice value to for each iteration of your array:
<cfset newPrice = (session.cart[5] * session.cart[3])>
<cfset totalPrice += totalPrice>
3) You could perform your subtotal calculation in your code where you need to display the total by looping through your array and generating the totals:
<cfset fSubTotal = 0>
<cfloop array="#session.cart#" item="arrCartItem">
<cfset fSubTotal += arrCartItem[3] * arrCartItem[5]>
</cfloop>
Copy link to clipboard
Copied
Thanks for the help. I had to alter the code, but you gave me the right direction to follow:
<cfset fSubtotal = 0>
<cfloop collection="#session.cart#" item="i">
<cfset fSubtotal += (session.cart[3] * session.cart[5])>
</cfloop>
It works perfectly.