Skip to content

Commit 4bd5562

Browse files
committed
Review fixes
1 parent 861ad2f commit 4bd5562

File tree

4 files changed

+108
-19
lines changed

4 files changed

+108
-19
lines changed

docs/discounts/discounts_api.md

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,46 @@ Discounts are applied in two places, listed in the [`DiscountType`](/api/php_api
3030
Regardless of activation place, discounts always apply to products and reduce their base price.
3131

3232
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.
3534

3635
### Rules
3736

38-
Discount rules define how the calculate the price reduction.
37+
Discount rules define how to calculate the price reduction.
3938
The following discount rule types are available in the `\Ibexa\Discounts\Value\DiscountRule` namespace:
4039

41-
| Rule type<br>(identifier) | Description | Expression value |
40+
| Rule type<br>(identifier) | Description | Required expression value |
4241
|---|---|---|
4342
| `FixedAmount`<br><nobr>(`fixed_amount`)</nobr> | Deducts the specified amount, for example <nobr>10 EUR</nobr>, from the base price | <nobr>`discount_amount`</nobr> |
4443
| `Percentage`<br><nobr>(`percentage`)</nobr> | Deducts the specified percentage, for example -10%, from the base price | <nobr>`discount_percentage`</nobr> |
4544

4645
Only a single discount can be applied to a given product, and a discount can only have a single rule.
4746

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):
55+
56+
``` yaml hl_lines="4-7"
57+
[[= include_file('code_samples/data_migration/examples/discounts/discount_create.yaml', 0, 2) =]]# ...
58+
[[= include_file('code_samples/data_migration/examples/discounts/discount_create.yaml', 18, 22) =]]
59+
```
60+
61+
- parsing responses from the [REST API](https://doc.ibexa.co/en/4.6/api/rest_api/rest_api_reference/rest_api_reference.html#discounts):
62+
63+
``` json hl_lines="16-21"
64+
[[= include_file('docs/api/rest_api/rest_api_reference/input/examples/discounts/Discount.json.example', 0, 21) =]]
65+
[[= include_file('docs/api/rest_api/rest_api_reference/input/examples/discounts/Discount.json.example', 44, 45) =]]
66+
```
67+
4868
### Conditions
4969

5070
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:
5171

52-
| Condition<br>(identifier) | Applies to | Description | Expression values |
72+
| Condition<br>(identifier) | Applies to | Description | Required expression values |
5373
|---|---|---|---|
5474
| <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` |
5575
| <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.
6282

6383
When multiple conditions are specified, all of them must be met.
6484

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):
93+
94+
``` yaml hl_lines="4-14"
95+
[[= include_file('code_samples/data_migration/examples/discounts/discount_create.yaml', 0, 2) =]]# ...
96+
[[= include_file('code_samples/data_migration/examples/discounts/discount_create.yaml', 22, 33) =]]
97+
```
98+
99+
- parsing responses from the [REST API](https://doc.ibexa.co/en/4.6/api/rest_api/rest_api_reference/rest_api_reference.html#discounts):
100+
101+
``` json hl_lines="16-23"
102+
[[= include_file('docs/api/rest_api/rest_api_reference/input/examples/discounts/Discount.json.example', 0, 15) =]][[= include_file('docs/api/rest_api/rest_api_reference/input/examples/discounts/Discount.json.example', 21, 29) =]]
103+
[[= include_file('docs/api/rest_api/rest_api_reference/input/examples/discounts/Discount.json.example', 44, 45) =]]
104+
```
105+
65106
### Priority
66107

67108
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.

docs/discounts/discounts_guide.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ You can also limit this choice to a subset of products:
9696

9797
#### Conditions
9898

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+
99106
For **cart discounts**, you can specify additional conditions that must be met for the discount to apply.
100107

101108
These conditions can include:
@@ -104,6 +111,8 @@ These conditions can include:
104111
- minimum purchase amount (total cart value)
105112
- special [discount codes](#discount-codes)
106113

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+
107116
##### Discount codes
108117

109118
For **cart discounts**, you can specify an additional text value that needs to be entered in the cart for the discount to apply.

docs/discounts/extend_discounts.md

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,28 @@ Together with the existing [events](event_reference.md) and the [Discounts PHP A
2020

2121
## Create custom conditions and rules
2222

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.
2424

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).
2626

2727
### Available expressions
2828

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).
3031

3132
| Type | Name | Value | Available for |
3233
| --- | --- | --- | --- |
3334
| 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 |
3435
| Function | `is_in_category()` | `true/false`, depending if a product belongs to given [product categories](pim_guide.md#product-categories).| Conditions, rules |
3536
| Function | `is_user_in_customer_group()` | `true/false`, depending if an user belongs to given [customer groups](customer_groups.md). | Conditions, rules |
3637
| 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 |
3840
| Variable | `cart` | [Cart object](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Cart-Value-CartInterface.html) associated with current context.| Conditions, rules |
3941
| Variable | `currency` | [Currency object](/api/php_api/php_api_reference/classes/Ibexa-Contracts-ProductCatalog-Values-CurrencyInterface.html) of the current siteaccess. | Conditions, rules |
4042
| 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 |
43+
| Variable | `product` | [Product object](/api/php_api/php_api_reference/classes/Ibexa-Contracts-ProductCatalog-Values-ProductInterface.html)| Conditions, rules |
4144
| Variable | `amount` | Original price of the product | Rules |
42-
| Variable | `product` | [Product object](/api/php_api/php_api_reference/classes/Ibexa-Contracts-ProductCatalog-Values-ProductInterface.html)| Rules |
4345

4446
### Custom expressions
4547

@@ -101,18 +103,27 @@ In a production implementation, you should consider refactoring the `current_use
101103

102104
### Implement custom condition
103105

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.
107108

108109
Create the condition by creating a class implementing the [`DiscountConditionInterface`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Discounts-Value-DiscountConditionInterface.html):
109110

110-
``` php
111+
``` php hl_lines="29-32"
111112
[[= include_file('code_samples/discounts/src/Discounts/Condition/IsAccountAnniversary.php') =]]
112113
```
113114

115+
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+
114118
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
116127

117128
For each custom condition class, you must create a dedicated condition factory, a class implementing the `\Ibexa\Discounts\Repository\DiscountCondition\DiscountConditionFactoryInterface` inteface.
118129

@@ -131,7 +142,16 @@ Mark it as a service using the `ibexa.discounts.condition.factory` service tag a
131142
discriminator: !php/const App\Discounts\Condition\IsAccountAnniversary::IDENTIFIER
132143
```
133144

134-
You can now use the condition using the PHP API.
145+
You can now use the condition, for example by using the PHP API or data migrations:
146+
147+
``` yaml hl_lines="16-19"
148+
[[= include_file('code_samples/data_migration/examples/discounts/discount_create.yaml', 0, 2) =]]# ...
149+
[[= include_file('code_samples/data_migration/examples/discounts/discount_create.yaml', 22, 33) =]]
150+
-
151+
identifier: is_account_anniversary
152+
expressionValues:
153+
tolerance: 5
154+
```
135155

136156
To learn how to integrate it into the back office, see [Extend Discounts wizard](extend_discounts_wizard.md).
137157

@@ -142,11 +162,19 @@ You could use it, for example, in regions sharing the same currency and apply th
142162

143163
To implement a custom rule, create a class implementing the [`DiscountRuleInterface`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Discounts-Value-DiscountRuleInterface.html).
144164

145-
146-
``` php
165+
``` php hl_lines="35-38"
147166
[[= include_file('code_samples/discounts/src/Discounts/Rule/PurchasingPowerParityRule.php') =]]
148167
```
149168

169+
The `getExpression` method contains the logic of the rule, expressed using the variables and functions available in the expression engine.
170+
The expression must return the new price of the product.
171+
172+
It uses three expressions:
173+
174+
- the built-in `amount` variable, holding the purchase amount
175+
- the built-in `get_current_region()` function, returning the current region
176+
- a custom `power_parity_map` variable, holding the purchasing power partity map. It's defined in the constuctor.
177+
150178
As with conditions, create a dedicated rule factory:
151179

152180
``` php

docs/discounts/extend_discounts_wizard.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,18 @@ They include:
3232
- [`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
3333
- [`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)
3434

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.
3647
See [discount's form events](discounts_events.md#form-events) for a list of the available events.
3748

3849
## Integrate custom conditions

0 commit comments

Comments
 (0)