Quantifiers (any, all, none)

Karl Falconer · Updated

A quantifier lets you ask a question about a collection. Do any line items match? Do all of them? Do none? The syntax is:

model has any|all|none collection with (condition)
when
  order has any order_lines with (order_line.sku starts_with "HAZMAT-")
then
  set order.custom.requires_signature to true
end

Reads as: when the order has any order_lines whose SKU starts with “HAZMAT-“, set the custom requires_signature field to true.

The three quantifiers

Quantifier Meaning
any At least one item in the collection matches.
all Every item matches (also true if the collection is empty).
none No item matches.

Examples

Any premium item:

when
  order has any order_lines with (order_line.sku starts_with "PREMIUM-")
then
  set order.custom.tier to "premium"
end

All items in stock:

when
  order has all order_lines with (order_line.qty_ordered - order_line.qty_fulfilled >= order_line.qty_ordered)
then
  set order.custom.fulfillable to true
end

No restricted items:

when
  order has none order_lines with (order_line.custom.is_restricted)
then
  accept order
end

Combining with other conditions

Quantifiers are just conditions — combine them with and, or, not like anything else.

when
  order.subtotal > 500.0
  and order has any order_lines with (order_line.gift_wrap)
then
  set order.custom.handling_tag to "fragile-priority"
end
when
  not (order has any order_lines with (order_line.custom.is_backorder))
then
  set order.custom.ready_to_ship to true
end

Multiple conditions inside a quantifier

The condition inside with (...) is a normal logical expression. Combine fields with and, or, not as usual.

when
  order has any order_lines with (
    order_line.sku starts_with "PREMIUM-"
    and order_line.qty_ordered > 1
  )
then
  ...
end

A subtle point: all on an empty collection

If a collection has zero items, all returns true — vacuously, because there’s nothing to fail the check. This is mathematically standard but can surprise people.

when
  order has all order_lines with (order_line.qty_ordered - order_line.qty_fulfilled >= 1)
then
  # This fires for an order with NO line items.
end

If you want to require at least one item and have all of them match, combine any and all:

when
  order has any order_lines with (true)
  and order has all order_lines with (order_line.qty_ordered - order_line.qty_fulfilled >= 1)
then
  ...
end

What collections you can quantify

Whatever model the rule is running against, the available collections come from that model. Common ones:

Model Collections
order order_lines
order_line inventories, options, comments
shipment shipment_lines
purchase_order purchase_order_items
sales_order_return sales_order_return_items

For deeper navigation into a single item — order_lines[0].comments[0].text and so on — see Collections and Source Data.

When to use a for loop instead

A quantifier asks a yes/no question about a collection. If you need to do something to each item (transform it, route it, set a value on it), use a for loop instead.

# Quantifier — checks "does any item start with HAZMAT?"
when
  order has any order_lines with (order_line.sku starts_with "HAZMAT-")
then
  set order.custom.requires_signature to true
end

# Loop — does something to every item
for order_lines in order
  when order_line.sku starts_with "HAZMAT-" then
    set order_line.custom.requires_signature to true
  end
end
Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.