File Formats - Formulas

When your customers Add or [AddLineItem] products to their shopping cart, WebDNA gets the price of the product in one of two ways: the price can come from a field in the database called "price," or it can be calculated based on a formula. To prevent "hacking," WebDNA never allows remote users to set product prices -- but you can still customize pricing by creating a formula to calculate a different price based on any WebDNA tags, such as [username], [zip], [math] calculation, or any form, text or math variable that is active on that page.

Note: The TeaRoom example uses a formula to calculate price, taxRate, unitShipCost and overall shipping costs based on the customer's shipping address.

Every time a product is added to the shopping cart, WebDNA calculates the item's price, taxable, and unitShipCost as follows:

  1. Look for a file called Formulas.db in the same folder as the shopping cart template itself, and look for a formula called "price."
  2. If Formulas.db contains a "price" formula, then evaluate the WebDNA expression (in the context of the current lineitem, so tags such as [price], [quantity], [zip] and [sku] are available, as well as any form, text or math variables active on that page).
  3. Set the price of the product based on the calculated formula, or if no formula is found, simply use the "price" field from the product database corresponding to the item's SKU.
  4. Repeat the 3 steps above for "unitShipCost."
  5. Repeat the 3 steps above for "taxable."
  6. Repeat the 3 steps above for "taxRate", which is applied to the entire order, not just the added item. If no formula is found, then look for a form variable called "taxRate" and use that instead.
  7. Repeat the 3 steps above for "shipCost", which is applied to the entire order, not just the added item. If no formula is found, then look for a form variable called "shipCost" and use that instead.

Variable
(case-sensitive)
Description
price
lookup SKU in database, or calculate from formula (applied only to this lineItem being added).
unitShipCost
lookup SKU in database, or calculate from formula (applied only to this lineItem being added).
taxRate
optional parameter to Add command, or calculate from formula (applied to entire order file).
shipCost
optional parameter to Add command, or calculate from formula (applied to entire order file). This number is added to the sum total of all the unitShipCost values to arrive at the shippingTotal for the whole order.
taxable
lookup SKU in database, or calculate from formula (applied only to this lineItem being added). The result of the formula must be either T or F.

The GeneralStore example uses the following formula for price:

[lookup db=catalog.txt&lookInField=sku&value=[sku]&returnField=price]

This formula looks up the price of the product in the database and returns it unchanged. This is for demonstration purposes only, because in this particular case, the formula calculates exactly the same price as though you had not specified a formula at all. To do something more complex, you might perform some calculation based on the visitor's [username] information, like so:

[math][showif [username]=GRANT]0.0*[/showif][lookup db=catalog.txt&lookInField=sku&value=[sku]&returnField=price][/math]

This example would set the price to $0 if the user was logged in as "GRANT", otherwise the price would be unchanged.

There should be no carraige returns, other than signifing line endings, in the formula. If carriage returns used (perhaps to make the formula more 'readable'), then they should be 'hidden' using comments ([!][/!]) to hide extra carraige returns. For example:

"[if [username]=DONOVAN][!]
[/!][then][math][_discount]*[lookup db=products.db&lookInField=sku&value=[sku]&returnField=price][/math][/then][!]
[/!][else][lookup db=products.db&lookInField=sku&value=[sku]&returnField=price][/else][/if]"

Example Shipping Cost Strategies
If your charges for shipping are... ...then the formulas would be as follows:
$6.95 + $2.00 per additional item shipCost 4.95 (in Formulas.db)
unitShipCost 2.00 (in Formulas.db)
When there is 1 item in the cart, the shipTotal will be 4.95+2.00 = 6.95
$15.00 flat shipCost 15.00 (in Formulas.db)
unitShipCost 0.00 (in Formulas.db)
$9.95 base charge + each item has its own handling charge (often based on weight) shipCost 9.95 (in Formulas.db)
"unitShipCost" field in your product database contains numeric cost for each item
Erase unitShipCost formula from Formulas.db, so product database field is used instead of formula.
When there is one item in the cart, the shipTotal will be 9.95 + [unitShipCost] taken from that SKU's record in the product database.
$15.00 flat in the state of NJ, $35.00 everywhere else shipCost [ShowIf [ShipToState]=NJ]15.00[/ShowIf][HideIf [ShipToState]=NJ]35.00[/HideIf]
unitShipCost 0.00 (in Formulas.db)
15% of the subtotal shipCost [math][subTotal]*.15[/math] (in Formulas.db)
unitShipCost 0.00 (in Formulas.db)