Shopping cart

Deploying to Mac OS? Ask Mac OS specific questions 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 » Mon Jul 20, 2009 7:56 pm

Wonder if somebody could helpe build a simple and easy 'shopping cart' like function for my software using studio 3.5.

Users will be able to select various mobile phone accessories from my application. I want to make it so that when a user highlights their chosen accessory they are then to click the 'add to cart' button, and for their selection to then be displayed in a 'list'.

Users will be able to add as many accessories as they like but for the life of me cannot work out the best way or best scrypt that tells runrev to add the selections to a 'list' when 'add to cart' button is pressed.

It's easy to create the scrypt that adds one but how do you make it so that when another accessory is added that it displays directly under the previous selection?

Could someone guide me in the right direction?

Thanks

Dan

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

Post by SparkOut » Mon Jul 20, 2009 8:22 pm

Presumably your cart is a list field where you can have tab delimited items that show the quantity selected, item description, unit price and line cost, or something like that? Then you simply want to have (eg)

Code: Select all

Qty    Desc                          Item Price      Line Total
2      widgets                       0.50             1.00
5      gizmos                        20.00            100.00
3      gizmerwidges                  99.99            299.97
Depending on where you collect the quantity and price etc, you would do something like

Code: Select all

put the hilitedLines of field "ItemsOnOffer" into theLines
repeat for each line tLine in theLines
  put line tLine of field "QuantitySelected" into tQty
  put line tLine of field "ItemsOnOffer" into tDesc
  put line tLine of field "ItemValue" into tItemPrice
  put tQty * tItemPrice into tLineTotal
  put tQty & tab & tDesc & tab & tItemPrice & tab & tLineTotal & cr after field "ShoppingCart"
  --format the prices for neatness, but your item justification is a bit limited in a list field anyway
end repeat
With a DataGrid you could do some very funky stuff and have an image of the item and description, line cost all set out with independent justification so that the prices are decimal aligned nicely etc.
Hope that gets you started anyway. There are of course many ways that Rev lets you skin the cat.

HTH

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

Post by bsouthuk » Tue Jul 21, 2009 2:20 am

Thanks very much for that, it works fine. The only thing i cant work out now is how to delete one of the lines - Is this possible?

Have also checked out the data grid and your right, very funky indeed. Am struggling using your scrypt for it to work on the data grid. Is this far more complicated or it it a case of amending your code slightly?

Thank you

Daniel

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

Post by SparkOut » Tue Jul 21, 2009 12:01 pm

It's as simple as

Code: Select all

delete line <theLineNumber> of field "ShoppingCart"
You might determine the line number by using lineOffset and matching a description, say, or the hilitedLines of field "ShoppingCart" (assuming you allow selection of the Shopping Cart field lines like that, and if you allow multiple lines, then you would loop through each value in the hilitedLines and delete each one).

Depending on how funky you want to get with a DataGrid it can be quite complicated, but although it may seem daunting on the surface, when you have worked through the tutorial material and practiced for a bit you will find them quite easy to understand, I'm sure. In the most basic form you can take build up the shopping cart lines in a variable and then

Code: Select all

set the dgText of group "ShoppingCartDataGrid" to theShoppingCartVariable
but since you're building up the items piece by piece, it would be better to use the DataGrid array notation (which is how the DataGrids work under the hood).
If (for instance) you were to add an empty tab stop at the point where you want an image in the DataGrid you could then edit the row template to have the behavior look up an image that matched the item description and display it. That's not as hard as it sounds - Trevor has got some great tutorial material that shows this sort of thing, and also look for the webinar video that shows what can be done.

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

Post by bsouthuk » Tue Jul 21, 2009 2:33 pm

Thats brilliant, I appreciate this a great deal. I think i'll stick to the simpler field style shopping cart for now.

The delete code by line number you gave works prefectly. However, when I try the code that deletes the line that is highlighted, I cant get it to work. The code I have tried is:

delete the hilitedLines of field "ShoppingCart"

I've tried some alterations but still it does not work. The highlighted function will work perfectly with my application and seems as though this is the final piece of the jigsaw once done!

Cheers

Dan

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

Post by SparkOut » Tue Jul 21, 2009 4:17 pm

Look up the info for "hilitedLines" - it doesn't mean a whole chunk of text, it returns a numeric value of the hilighted line (or list of values, if multiple lines are highlighted).
So you need to

Code: Select all

delete LINE (the hilitedLines of field "ShoppingCart") of field "ShoppingCart"
the parentheses are only there to show you that expression resolves to a line number that can then be used to delete the line number specified from the field. It looks odd, but you're not specifying field "ShoppingCart" twice - one reference is only to resolve the line number.
Or if multiple line selection is permitted

Code: Select all

put the hilitedLines of field "ShoppingCart" into theLineNumbersToDelete
sort theLineNumbersToDelete numeric descending
--otherwise when you delete a line, say 2, all the other lines will move up
--and when you come to delete a lower line, say 4, it will have moved
--into line 3 and line 5 will be deleted instead
repeat for each line tDeleteThisLine of theLineNumbersToDelete
  delete line tDeleteThisLine of field "ShoppingCart"
end repeat

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

Post by bsouthuk » Tue Jul 21, 2009 4:48 pm

Brilliant - absolutely brilliant!

Thank you very much

Daniel

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

Post by SparkOut » Tue Jul 21, 2009 5:02 pm

Glad that helps. Of course you may need to do a bit more error trapping and checking the values, but this should give you a good place to start.

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

Post by bsouthuk » Wed Jul 22, 2009 2:26 pm

Yes, i've quality controlled it fully now but need it to perform one more thing.

I want it to total the "ItemValue" collumn in a seperate field i've called "totalvalue". Is this easily done?

Cheers

Daniel

Klaus
Posts: 14206
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Post by Klaus » Fri Jul 24, 2009 3:19 pm

Hi Daniel,

you can loop through your shopping card list like this, assumed the values you wnat to add are in column 3 in a TAB delimited field "shopping cart list":

Code: Select all

...
set itemdel to TAB
put empty into tTotals1
repeat for each line i in fld "shopping cart list"
  if item 3 of i <> empty then
       add item 3 of i to tTotals1
  end if
end repeat
put tTotals1 into fld "totalvalue"
...
Best

Klaus

Post Reply