Skip to content

Commit 6742514

Browse files
committed
Added a note about variable vs function
1 parent 1f73465 commit 6742514

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

docs/discounts/extend_discounts.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ Mark it as a service using the `ibexa.discounts.expression_language.function` se
8484

8585
Two new expressions are now available for use in custom conditions and rules.
8686

87+
When deciding whether to register a new custom variable or function, consider the following:
88+
89+
- variables are always evaluated by the expression engine and the result is available for all the rules and conditions specified in the discount
90+
- functions are invoked only when the rule or condition using them is evaluated. If there are multiple conditions using them, they will be invoked multiple times
91+
92+
For performance reasons, it's recommended to:
93+
94+
- use variables only for lightweight calculations
95+
- use functions for resource-intensive calculations (for example, checking customer's order history)
96+
- implement caching (for example, in-memory) for function results to avoid redundant calculations within a single discount, if the function is used multiple times
97+
- specify the most resource-intensive conditions as the last to evaluate. As all conditions must be met for the discount to apply, it's possible to skip evaluating them if the previous ones won't be met
98+
99+
In a production implementation, you should consider refactoring the `current_user_registration_date` variable into a `get_current_user_registration_date` function to avoid always loading the current user object and improve performance.
100+
87101
### Implement custom condition
88102

89103
The following example creates a new discount condition. It allows you to offer a special discount for customers on the date when their account was created, making use of the expressions added above.

0 commit comments

Comments
 (0)