Shopping cart

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
bsouthuk
Posts: 261
Joined: Fri Dec 05, 2008 7:25 pm

Shopping cart

Post by bsouthuk » Thu Jul 23, 2009 5:41 pm

can anybody help me with some scrypt?

Im working on a simple shopping cart for my application and am using the following code:

Code: Select all

on mouseUp
put the hilitedLines of field "ItemsOnOffer" into theLines 
repeat for each line tLine in theLines 
   set numberformat to 0.00
   put line tLine of field "ItemsOnOffer" into tDesc 
   put field "QuantitySelected" into tQty 
put field "ItemValue" into tItemPrice 
put tQty * tItemPrice into tLineTotal 
put tDesc & tab & tab & tQty & tab & tItemPrice & tab & tLineTotal & cr after field "cart" stack "accessory selections"
put "" into field "Quantityselected"
put "" into field "itemvalue"
--format the prices for neatness, but your item justification is a bit limited in a list field anyway 
end repeat
end mouseUp

I want to add a seperate field that calculates the grand total. So far the code works out the total for each line and displays it in the table field but what code could i use that calculates all of the lines and displays the total in a seperate field? I'm very close in throwing my laptop out the window! lol

Your help would be most appreciated.

Daniel

SparkOut
Posts: 2949
Joined: Sun Sep 23, 2007 4:58 pm

Post by SparkOut » Thu Jul 23, 2009 5:57 pm

You can add a new field "CartTotal" (or such) to the card and inside the repeat loop add the line

Code: Select all

add tLineTotal to field "CartTotal"
I'm not sure where your extra 'stack "accessory selections' reference came from, but it seems out of place. If your script needs to identify the card and stack then the line would make more sense as something like:

Code: Select all

put tDesc & tab & tab & tQty & tab & tItemPrice & tab & tLineTotal & cr after field "cart" of card "shopping cart" of stack "accessory selections"
Alternatively, if you want to add the items and then have an "Update Total" button, you could have the button:

Code: Select all

local tCartTotal
set the itemDelimiter to tab
repeat for each line tLine in field "cart"
  add the last item of tLine to tCartTotal
  --assuming your line total is still the last item in each line of the field
end repeat
put tCartTotal into field "CartTotal"
(Edited the above code to replace "of" with "in" in the repeat loop. See below.)
Last edited by SparkOut on Thu Jul 23, 2009 10:44 pm, edited 1 time in total.

bsouthuk
Posts: 261
Joined: Fri Dec 05, 2008 7:25 pm

Post by bsouthuk » Thu Jul 23, 2009 6:34 pm

Thanks again SparkOut!

I like the idea of an "update total button" but when i tried the scrypt it didnt work.

When i compiled an error message appeared

"button "Button": compilation error at line 4 (repeat: missing 'in'), char 22"

i've tried 1 or 2 things but no joy.

What do you think?

SparkOut
Posts: 2949
Joined: Sun Sep 23, 2007 4:58 pm

Post by SparkOut » Thu Jul 23, 2009 10:43 pm

:oops: I think I've done something silly - try replacing "of" with "in" in that line to read

Code: Select all

repeat for each line tLine IN field "cart"
(edited above code so that it's not a problem for anyone else who might be learning from this thread in future and perhaps copy paste the wrong thing).

Post Reply