Skip to main content

Price rules

Price rules calculate a pricelist's sales price from the price fields you store: a markup on cost, a discount on the catalogue price, a different calculation for one brand. They are short expressions, not a scripting language, so they are predictable and always give the same result.


Defining rules​

Rules are part of the pricelist, in price_rules:

json
{
"code": "wholesale-se",
"currency_code": "SEK",
"vat_mode": "NET",
"parent_code": "base-se",
"price_rules": [
{ "code": "acme-brand", "filter": "brand = 'acme'", "expression": "prices.cost * 1.25", "description": "Acme at cost + 25 %" },
{ "code": "default", "filter": "", "expression": "sales * 0.85", "description": "15 % off" }
]
}
FieldDescription
codeIdentifies the rule.
filterThe articles the rule applies to, selected by their metadata. A blank filter applies to all articles. See Filters.
expressionThe calculation, written in CEL. See Expressions.
descriptionFree text.

For each price, the rules are tried in order. The first rule whose filter matches the article and whose expression returns a number sets the sales price. If no rule does, the price keeps the sales price it started with: the fixed field for a price written on the pricelist, or the parent's price for an inherited one.


Filters​

A filter selects articles by their metadata:

text
brand = 'acme'
category = 'chairs' and brand != 'acme'
(season = 'summer' or season = 'clearance') and not outlet = 'yes'
  • field = 'value' matches when any of the article's values for field is value. field != 'value' matches when none of them is.
  • Combine conditions with and, or, not and parentheses. Keywords are case-insensitive, and values are in single quotes.
  • A blank filter matches every article.

Rules apply only to SKUs that exist as articles. A price for a SKU without an article uses its fixed field as the sales price.


Expressions​

Available variables​

VariableTypeDescription
pricesMap of price field to amountThe price's named fields, for example prices.fixed, prices.cost, prices.catalog
salesAmountThe sales price going into the rule: the fixed field for a price written on this pricelist, or the parent's sales price for an inherited price
parentMap of metadata key to list of valuesThe parent pricelist's metadata, if the pricelist has a parent

An expression that returns a number sets the sales price, kept to four decimal places and rounded half up. Any other result (null, true, a string) means the rule does not apply, and the next rule is tried. An expression that fails, for example because it reads a price field the price does not have, stops rule processing for that price, and the price keeps the sales price it started with.

Examples​

text
prices.cost * 1.35 // 35% markup on cost
prices.catalog * 0.90 // 10% discount on catalog
sales - 5 // 5 off
prices.fixed > 100.0 ? prices.fixed * 0.95 : prices.fixed // 5% off for items over 100
parent['segment'][0] == 'vip' ? sales * 0.9 : sales // 10% off under a VIP parent

On an inherited price, sales * 0.9 takes 10 % off the parent's price, so rules on several levels of a hierarchy compound: 100 on the base pricelist, 90 on a segment with sales * 0.9, and 81 on a customer below it with sales * 0.9. To start from a stored field instead, use prices.<field>. See Inheritance.

Amounts are exact decimals: prices.fixed / 3 and sales - 5 have no floating-point errors, and prices.a == prices.b is true whenever the two are numerically equal (100 and 100.00).

An expression that does not compile, or that uses a variable other than prices, sales and parent, is rejected when the pricelist is saved. Filters are not checked when the pricelist is saved: a filter that cannot be parsed is accepted, but its rule never applies. A rule with a blank expression is ignored. Other mistakes, such as comparing an amount with a string, appear only when a price is calculated: rule processing then stops, and the price keeps the sales price it started with. Test a new rule with a price query.


When rules take effect​

  • New prices: a price is calculated with the rules in force when it takes effect.
  • Changed rules: the pricelist's own prices and the prices it inherits are recalculated with the new rules from the moment the change takes effect, either now or at its valid_from. Prices before that moment keep the rules they had. See Historical pricing.

Limitations​

Rules work on one price at a time. They do not:

  • read other SKUs' prices, external data or the time of day;
  • depend on the query (customer, quantity, currency). Select the customer's pricelist in the query instead, and use quantity breaks for volume pricing;
  • handle date ranges. Schedule prices with a start date instead; see Scheduled updates.

Summary​

A pricelist's rules turn the price fields you store into its sales price. A metadata filter selects the articles, a CEL expression performs the calculation, and the first matching rule applies.