You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/commerce/4.x/discounts.md
+29-34Lines changed: 29 additions & 34 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -104,60 +104,55 @@ Restrict the discount to a specific time period defined by start and end date fi
104
104
105
105
### Order Condition Formula
106
106
107
-
The order condition formula lets you use a simple Twig condition syntax to add a matching rule to the discount.
108
-
If the field is left blank, the condition will match the order being matched to the discount (the other conditions will still apply).
107
+
The order condition formula lets you define custom logic via a pared-down [Twig expression](https://twig.symfony.com/doc/3.x/templates.html#expressions) that returns `true` or `false`. Formulas are ultimately evaluated as part of an `{% if ... %}` control tag.
109
108
110
-
The field accepts the [Twig’s expression syntax](https://twig.symfony.com/doc/3.x/templates.html#expressions), which is an expression that returns `true` or `false`.
109
+
- If the field is left blank it has no effect on the matching.
110
+
- If the expression evaluates to `true` (or any equivalent “[truthy](https://twig.symfony.com/doc/3.x/tags/if.html)” value), the discount _matches_ the order.
111
+
- If the expression evaluates to `false` (or any equivalent “falsey” value) the condition _disqualifies_ the order.
111
112
112
-
If the expression is calculated as `true`, the discount matches the order. If not, the condition disqualifies the order from the discount. An empty condition is the same as a `true` expression.
113
-
114
-
The condition formula can use an `order` variable, which for safety is an array and not the order element—it’s the same representation of the order you’d see if you exported it from the order index page. This data-only format prevents a store manager from accidentally calling methods like `order.markAsComplete()`.
113
+
The condition formula has access to an `order` variable, which (for safety) is an array and not the order element—effectively the same representation of the order you’d see if you exported it from the order index page. This data-only format prevents a store manager from accidentally or maliciously calling methods like `order.markAsComplete()`. The same is true for any nested elements and custom field values—everything is serialized into arrays and scalar values.
115
114
116
115
::: tip
117
-
The condition formula’s `order` array is generated with:
As an additional safeguard, only a handful of Twig filters, functions, tags, and variables are available in this context. See <commerce4:craft\commerce\services\Formulas> for a complete list of allowed features.
124
117
:::
125
118
126
119
Here are some examples of an discount’s condition formula:
127
120
128
-
**Example 1:**
121
+
Example 1: Company Email Discount
129
122
130
-
```twig
131
-
'@myclient.com' in order.email
132
-
```
123
+
: ```twig
124
+
'@myclient.com' in order.email
125
+
```
133
126
134
-
The above would be a `true` statement if the order’s email contains the string `@myclient.com`.
127
+
The above would be a `true` statement if the order’s email contains the string `@myclient.com`.
135
128
136
-
This would be a way of giving this discount to anyone from that company.
129
+
This would be a way of giving a discount to customers using a specific company’s email address.
137
130
138
-
**Example 2:**
131
+
::: warning
132
+
Guest customers do not need to verify the email they use on an order! You may need to use the **Match Customer** condition builder to ensure they are a registered user.
133
+
:::
139
134
140
-
```twig
141
-
order.shippingAddressId and order.shippingAddress.zipCode[0:2] == '70'
142
-
```
135
+
Example 2: Local Customers
143
136
144
-
The above would be a `true` statement if the order has a shipping address and the shipping address `zipCode` starts with `70`.
137
+
: ```twig
138
+
order.shippingAddressId and order.shippingAddress.postalCode[0:2] == '70'
139
+
```
145
140
146
-
This would be a way of giving this discount to anyone shipping to that postal code.
141
+
The above would be a `true` statement if the order has a shipping address and the shipping address `postalCode` starts with `70`.
147
142
148
-
**Example 3:**
143
+
This would be a way of giving this discount to anyone shipping to that postal code.
149
144
150
-
```twig
151
-
order.myCustomLicenseField and order.myCustomLicenseField == 'Supporter'
152
-
```
145
+
Example 3: Custom Fields
153
146
154
-
The above would be a `true` statement if the order has a custom `myCustomLicenseField` field with a value of `Supporter`.
147
+
: ```twig
148
+
order.myCustomLicenseField and order.myCustomLicenseField == 'Supporter'
149
+
```
155
150
156
-
This would be a way of giving this discount to anyone that’s chosen a specific product license, and you could similarly use it to offer discounts that depend on custom field data.
151
+
The above would be a `true` statement if the order has a custom `myCustomLicenseField` field with a value of `Supporter`.
157
152
158
-
::: tip
159
-
For safety, only a serialized representation of order attributes is available; you can’t call custom field methods from a condition formula.
160
-
:::
153
+
This would be a way of giving this discount to anyone that’s chosen a specific product license, and you could similarly use it to offer discounts that depend on custom field data.
154
+
155
+
Whenever possible, we recommend implementing **Order Condition Formula** as **Match Order** conditions. Doing so means that we can automatically migrate conditions or provide suggestions during major version upgrades.
Copy file name to clipboardExpand all lines: docs/commerce/5.x/system/discounts.md
+29-34Lines changed: 29 additions & 34 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -72,60 +72,55 @@ Restrict the discount to a specific time period defined by start and end date fi
72
72
73
73
### Order Condition Formula
74
74
75
-
The order condition formula lets you use a simple Twig condition syntax to add a matching rule to the discount.
76
-
If the field is left blank, the condition will match the order being matched to the discount (the other conditions will still apply).
75
+
The order condition formula lets you define custom logic via a pared-down [Twig expression](https://twig.symfony.com/doc/3.x/templates.html#expressions) that returns `true` or `false`. Formulas are ultimately evaluated as part of an `{% if ... %}` control tag.
77
76
78
-
The field accepts the [Twig’s expression syntax](https://twig.symfony.com/doc/3.x/templates.html#expressions), which is an expression that returns `true` or `false`.
77
+
- If the field is left blank it has no effect on the matching.
78
+
- If the expression evaluates to `true` (or any equivalent “[truthy](https://twig.symfony.com/doc/3.x/tags/if.html)” value), the discount _matches_ the order.
79
+
- If the expression evaluates to `false` (or any equivalent “falsey” value) the condition _disqualifies_ the order.
79
80
80
-
If the expression is calculated as `true`, the discount matches the order. If not, the condition disqualifies the order from the discount. An empty condition is the same as a `true` expression.
81
-
82
-
The condition formula can use an `order` variable, which for safety is an array and not the order element—it’s the same representation of the order you’d see if you exported it from the order index page. This data-only format prevents a store manager from accidentally calling methods like `order.markAsComplete()`.
81
+
The condition formula has access to an `order` variable, which (for safety) is an array and not the order element—effectively the same representation of the order you’d see if you exported it from the order index page. This data-only format prevents a store manager from accidentally or maliciously calling methods like `order.markAsComplete()`. The same is true for any nested elements and custom field values—everything is serialized into arrays and scalar values.
83
82
84
83
::: tip
85
-
The condition formula’s `order` array is generated with:
As an additional safeguard, only a handful of Twig filters, functions, tags, and variables are available in this context. See <commerce5:craft\commerce\services\Formulas> for a complete list of allowed features.
92
85
:::
93
86
94
87
Here are some examples of an discount’s condition formula:
95
88
96
-
**Example 1:**
89
+
Example 1: Company Email Discount
97
90
98
-
```twig
99
-
'@myclient.com' in order.email
100
-
```
91
+
: ```twig
92
+
'@myclient.com' in order.email
93
+
```
101
94
102
-
The above would be a `true` statement if the order’s email contains the string `@myclient.com`.
95
+
The above would be a `true` statement if the order’s email contains the string `@myclient.com`.
103
96
104
-
This would be a way of giving this discount to anyone from that company.
97
+
This would be a way of giving a discount to customers using a specific company’s email address.
105
98
106
-
**Example 2:**
99
+
::: warning
100
+
Guest customers do not need to verify the email they use on an order! You may need to use the **Match Customer** condition builder to ensure they are a registered user.
101
+
:::
107
102
108
-
```twig
109
-
order.shippingAddressId and order.shippingAddress.zipCode[0:2] == '70'
110
-
```
103
+
Example 2: Local Customers
111
104
112
-
The above would be a `true` statement if the order has a shipping address and the shipping address `zipCode` starts with `70`.
105
+
: ```twig
106
+
order.shippingAddressId and order.shippingAddress.postalCode[0:2] == '70'
107
+
```
113
108
114
-
This would be a way of giving this discount to anyone shipping to that postal code.
109
+
The above would be a `true` statement if the order has a shipping address and the shipping address `postalCode` starts with `70`.
115
110
116
-
**Example 3:**
111
+
This would be a way of giving this discount to anyone shipping to that postal code.
117
112
118
-
```twig
119
-
order.myCustomLicenseField and order.myCustomLicenseField == 'Supporter'
120
-
```
113
+
Example 3: Custom Fields
121
114
122
-
The above would be a `true` statement if the order has a custom `myCustomLicenseField` field with a value of `Supporter`.
115
+
: ```twig
116
+
order.myCustomLicenseField and order.myCustomLicenseField == 'Supporter'
117
+
```
123
118
124
-
This would be a way of giving this discount to anyone that’s chosen a specific product license, and you could similarly use it to offer discounts that depend on custom field data.
119
+
The above would be a `true` statement if the order has a custom `myCustomLicenseField` field with a value of `Supporter`.
125
120
126
-
::: tip
127
-
For safety, only a serialized representation of order attributes is available; you can’t call custom field methods from a condition formula.
128
-
:::
121
+
This would be a way of giving this discount to anyone that’s chosen a specific product license, and you could similarly use it to offer discounts that depend on custom field data.
122
+
123
+
Whenever possible, we recommend implementing **Order Condition Formula** as **Match Order** conditions. Doing so means that we can automatically migrate conditions or provide suggestions during major version upgrades.
0 commit comments