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/discounts/discounts_api.md
+46-5Lines changed: 46 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,26 +30,46 @@ Discounts are applied in two places, listed in the [`DiscountType`](/api/php_api
30
30
Regardless of activation place, discounts always apply to products and reduce their base price.
31
31
32
32
To define when a discount activates and how the price is reduced, use rules and conditions.
33
-
They make use of the [Symfony Expression language]([[= symfony_doc=]]//components/expression_language.html).
34
-
Use the expression values provided below when using data migrations or when parsing REST API responses.
33
+
They use the [Symfony Expression language]([[= symfony_doc=]]/components/expression_language.html) to express their logic.
35
34
36
35
### Rules
37
36
38
-
Discount rules define how the calculate the price reduction.
37
+
Discount rules define how to calculate the price reduction.
39
38
The following discount rule types are available in the `\Ibexa\Discounts\Value\DiscountRule` namespace:
40
39
41
-
| Rule type<br>(identifier) | Description |Expression value |
40
+
| Rule type<br>(identifier) | Description |Required expression value |
42
41
|---|---|---|
43
42
|`FixedAmount`<br><nobr>(`fixed_amount`)</nobr> | Deducts the specified amount, for example <nobr>10 EUR</nobr>, from the base price | <nobr>`discount_amount`</nobr> |
44
43
|`Percentage`<br><nobr>(`percentage`)</nobr> | Deducts the specified percentage, for example -10%, from the base price | <nobr>`discount_percentage`</nobr> |
45
44
46
45
Only a single discount can be applied to a given product, and a discount can only have a single rule.
47
46
47
+
When creating a rule through other means than the user interface, you must pass the required expression values for the rule to be valid:
48
+
49
+
- using PHP, the values are passed through the constuctor which converts them into an expression variable
50
+
- using data migrations and the REST API, the values are specified using the `expressionValues` key
51
+
52
+
See the following examples for data migrations and the REST API usage:
53
+
54
+
- creating discounts with [data migrations](importing_data.md#discounts):
With conditions you can narrow down the scenarios in which the discount applies. The following conditions are available in the `\Ibexa\Discounts\Value\DiscountCondition` and `\Ibexa\DiscountsCodes\Value\DiscountCondition` namespaces:
51
71
52
-
| Condition<br>(identifier) | Applies to | Description |Expression values |
| <nobr>`IsInCategory`</nobr><br><nobr>(`is_in_category`)</nobr> | Cart, Catalog | Checks if the product belongs to specified [product categories]([[= user_doc =]]/pim/work_with_product_categories) |`categories`|
55
75
| <nobr>`IsInCurrency`</nobr><br><nobr>(`is_in_currency`)</nobr> | Cart, Catalog | Checks if the product has price in the specified currency | <nobr>`currency_code`</nobr> |
@@ -62,6 +82,27 @@ With conditions you can narrow down the scenarios in which the discount applies.
62
82
63
83
When multiple conditions are specified, all of them must be met.
64
84
85
+
As with rules, when creating a condition through other means than the user interface, you must pass the required expression values for the condition to be valid:
86
+
87
+
- using PHP, the values are passed through the constuctor which converts them into an expression variable
88
+
- using data migrations and the REST API, the values are specified using the `expressionValues` key
89
+
90
+
See the following examples for data migrations and the REST API usage:
91
+
92
+
- creating discounts with [data migrations](importing_data.md#discounts):
You can set discount priority as a number between 1 and 10 to indicate which discount should have [higher priority](discounts_guide.md#discounts-priority) when choosing the one to apply.
Copy file name to clipboardExpand all lines: docs/discounts/discounts_guide.md
+9Lines changed: 9 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -96,6 +96,13 @@ You can also limit this choice to a subset of products:
96
96
97
97
#### Conditions
98
98
99
+
Use conditions to limit the applicability of a discount, for example by checking that:
100
+
101
+
- the product belongs to a specific category
102
+
- the customer belongs to a specific customer group
103
+
104
+
before applying the discount.
105
+
99
106
For **cart discounts**, you can specify additional conditions that must be met for the discount to apply.
100
107
101
108
These conditions can include:
@@ -104,6 +111,8 @@ These conditions can include:
104
111
- minimum purchase amount (total cart value)
105
112
- special [discount codes](#discount-codes)
106
113
114
+
See [the built-in list of conditions](discounts_api.md#conditions) and [creating custom conditions](extend_discounts.md#implement-custom-condition) for more information.
115
+
107
116
##### Discount codes
108
117
109
118
For **cart discounts**, you can specify an additional text value that needs to be entered in the cart for the discount to apply.
Copy file name to clipboardExpand all lines: docs/discounts/extend_discounts.md
+41-13Lines changed: 41 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,26 +20,28 @@ Together with the existing [events](event_reference.md) and the [Discounts PHP A
20
20
21
21
## Create custom conditions and rules
22
22
23
-
With custom [conditions](discounts_api.md#conditions) you can create more advanced discounts that apply only in specific scenarios.
23
+
With custom [conditions](discounts_api.md#conditions)and [rules](discounts_api.md#rules)you can create more advanced discounts that apply only in specific scenarios.
24
24
25
-
The logic for both the conditions and rules is specified using[Symfony's expression language](https://symfony.com/doc/current/components/expression_language.html).
25
+
For both of them, you need to specify their logic with[Symfony's expression language](https://symfony.com/doc/current/components/expression_language.html).
26
26
27
27
### Available expressions
28
28
29
-
The following expressions are available for conditions and rules:
29
+
You can use the following built-in expressions (variables and functions) in your own custom conditions and rules.
30
+
You can also [create your own](#custom-expressions).
30
31
31
32
| Type | Name | Value | Available for |
32
33
| --- | --- | --- | --- |
33
34
| Function |`get_current_region()`|[Region object](/api/php_api/php_api_reference/classes/Ibexa-Contracts-ProductCatalog-Values-RegionInterface.html) of the current siteaccess.| Conditions, rules |
34
35
| Function |`is_in_category()`|`true/false`, depending if a product belongs to given [product categories](pim_guide.md#product-categories).| Conditions, rules |
35
36
| Function |`is_user_in_customer_group()`|`true/false`, depending if an user belongs to given [customer groups](customer_groups.md). | Conditions, rules |
36
37
| Function |`calculate_purchase_amount()`| Purchase amount, calculated for all products in the cart before the discounts are applied.| Conditions, rules |
37
-
| Function | <nobr>`is_product_in_product_codes()`</nobr> |`true/false`, depending if the product is part of the given list.| Conditions, rules |
38
+
| Function | <nobr>`is_product_in_product_codes()`</nobr> | Parameters: <br> - [Product object](/api/php_api/php_api_reference/classes/Ibexa-Contracts-ProductCatalog-Values-ProductInterface.html)<br>- array of product codes<br> Returns `true` if the product is part of the given list.| Conditions, rules |
39
+
| Function | <nobr>`is_valid_discount_code()`</nobr> | Parameter: discount code (string). <br> Returns `true` if the discount code is valid for current user.| Conditions, rules |
38
40
| Variable |`cart`|[Cart object](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Cart-Value-CartInterface.html) associated with current context.| Conditions, rules |
39
41
| Variable |`currency`|[Currency object](/api/php_api/php_api_reference/classes/Ibexa-Contracts-ProductCatalog-Values-CurrencyInterface.html) of the current siteaccess. | Conditions, rules |
40
42
| Variable |`customer_group`|[Customer group object](/api/php_api/php_api_reference/classes/Ibexa-Contracts-ProductCatalog-Values-CustomerGroupInterface.html) associated with given price context or the current user.| Conditions, rules |
@@ -101,18 +103,27 @@ In a production implementation, you should consider refactoring the `current_use
101
103
102
104
### Implement custom condition
103
105
104
-
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.
105
-
106
-
The `tolerance` option allows you to make the discount usable for a longer period of time (for example, a day before or after the registration date) to allow more time for the customers to use it.
106
+
The following example creates a new discount condition.
107
+
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.
107
108
108
109
Create the condition by creating a class implementing the [`DiscountConditionInterface`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Discounts-Value-DiscountConditionInterface.html):
This condition can be used in both catalog and cart discounts.
116
+
To implement a cart-only discount, additionally implement the marker [`CartDiscountConditionInterface`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Discounts-Value-CartDiscountConditionInterface.html) interface.
117
+
114
118
The `tolerance` option is made available for usage in the expression by passing it in the constructor.
115
-
The expression can evaluate to `true` or `false` depending on the custom expressions values.
119
+
The `getExpression` method contains the logic of the condition, expressed using the variables and functions available in the expression engine.
120
+
The expression must evaluate to `true` or `false`, indicating whether the condition is met.
121
+
122
+
The example uses three expressions:
123
+
124
+
- the custom `is_anniversary` function, returning a value indicating whether today is user's registration anniversary
125
+
- the custom `current_user_registration_date` variable, holding the value of current user's registration date
126
+
- the custom `tolerance` variable, holding the acceptable tolerance (in days) for the calculation
116
127
117
128
For each custom condition class, you must create a dedicated condition factory, a class implementing the `\Ibexa\Discounts\Repository\DiscountCondition\DiscountConditionFactoryInterface` inteface.
118
129
@@ -131,7 +142,16 @@ Mark it as a service using the `ibexa.discounts.condition.factory` service tag a
To learn how to integrate it into the back office, see [Extend Discounts wizard](extend_discounts_wizard.md).
137
157
@@ -142,11 +162,19 @@ You could use it, for example, in regions sharing the same currency and apply th
142
162
143
163
To implement a custom rule, create a class implementing the [`DiscountRuleInterface`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Discounts-Value-DiscountRuleInterface.html).
Copy file name to clipboardExpand all lines: docs/discounts/extend_discounts_wizard.md
+12-1Lines changed: 12 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -32,7 +32,18 @@ They include:
32
32
-[`mapUpdateDataToStruct()`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Discounts-DiscountFormMapperInterface.html#method_mapUpdateDataToStruct) creates the [`DiscountUpdateStruct`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Discounts-Value-Struct-DiscountUpdateStruct.html) object to update the discount
33
33
-[`mapEditTranslateDataToStruct()`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Discounts-DiscountFormMapperInterface.html#method_mapEditTranslateDataToStruct) creates the [`TranslationStruct`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Discounts-Value-Struct-DiscountTranslationStruct.html) objects for [translating the discounts](discounts_api.md#discount-translations)
34
34
35
-
In addition to these methods, the main form mapper and the form mappers responsible for each step in the wizard dispatch events that you can use to add your custom logic.
35
+
In the UI, the discounts wizard consists of several steps:
36
+
37
+
- General properties
38
+
- Target group
39
+
- Products
40
+
- Conditions (only for Cart discounts)
41
+
- Discount value
42
+
- Summary
43
+
44
+
Each of these steps is represented by its own form mappers, data classes, and form types in the code.
45
+
46
+
In addition, the main form mapper and the form mappers responsible for each step in the wizard dispatch events that you can use to add your custom logic.
36
47
See [discount's form events](discounts_events.md#form-events) for a list of the available events.
0 commit comments