Predicate language
Predicate language
A predicate is a boolean condition used by a commerce rule. It answers whether a cart, line item, or price record matches that rule. It does not query resources or replace the rule’s other configuration.
Select the right context
| Rule | Predicate reads |
|---|---|
| Cart discount, shipping method or rate, payment gateway | Cart context |
| Cart discount line-item target | One line item |
| Product discount | Product-price context |
A search clause such as tag:lighting is not a predicate.
A full-cart expression using line_items.any(...) is not a line-item target expression.
The target already represents one line.
Syntax
Use ==, !=, >, >=, <, and <= for comparisons.
Use &&, ||, and ! for boolean logic, with parentheses to make grouping explicit.
Supported contexts use snake_case fields, string literals, numbers, booleans, and null.
Collections support the methods documented by their context, including Contains, any, and count.
Compare money in minor units and pair thresholds with currency.
Guard nullable values before reading nested fields.
Copy resource IDs from Thor and use the expected casing for codes and strings.
Write true explicitly when a rule intentionally matches every eligible context.
Test intent, not only syntax
Start with one condition and verify it through the supported Admin validation or save operation. Then test a matching case, a non-matching case, and the boundary value. For cart rules, include anonymous customers and missing addresses. For product-price rules, include prices with and without optional dimensions.
A matching predicate only establishes eligibility. Scheduling, discount targets, priority, and combination settings can still change the result. Use Create a discount campaign for a complete workflow.