|
| 1 | +--- |
| 2 | +description: Discounts LTS Update enables reducing prices on products or product categories based on a detailed logic resolution. |
| 3 | +month_change: true |
| 4 | +editions: |
| 5 | + - lts-update |
| 6 | + - commerce |
| 7 | +--- |
| 8 | + |
| 9 | +# Discounts API |
| 10 | + |
| 11 | +## Manage discounts and discount codes |
| 12 | + |
| 13 | +By integrating with the [Discount feature](discounts_guide.md) you can automate the process of managing discounts, streamlining the whole process and automating business rules. |
| 14 | + |
| 15 | +For example, you can automatically create a discount when a customer places their 3rd order, encouraging them to make another purchase and increase their chances of becoming a local customer. |
| 16 | + |
| 17 | +You can manage discounts using [data migrations](importing_data.md#discounts), [REST API](/api/rest_api/rest_api_reference/rest_api_reference.html#discounts), or the PHP API by using the [Ibexa\Contracts\Discounts\DiscountServiceInterface](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Discounts-DiscountServiceInterface.html) service. |
| 18 | + |
| 19 | +The core concepts when working with discounts through the APIs are listed below. |
| 20 | + |
| 21 | +### Types |
| 22 | + |
| 23 | +When using the PHP API, the discount type defines where the discount can be applied. |
| 24 | + |
| 25 | +Discounts are applied in two places, listed in the [DiscountType](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Discounts-Value-DiscountType.html) class: |
| 26 | + |
| 27 | +- **Product catalog** - `catalog` discounts are activated when browsing the product catalog and do not require any action from the customer to be activated |
| 28 | +- **Cart** - `cart` discounts can activate when entering the [cart](cart.md), if the right conditions are met. They may also require entering a discount code to be activated |
| 29 | + |
| 30 | +Regardless of activation place, discounts always apply to products and reduce their base price. |
| 31 | + |
| 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 the REST API to pass the right values. |
| 35 | + |
| 36 | +### Rules |
| 37 | + |
| 38 | +Discount rules define how the calculate the price reduction. |
| 39 | +The following discount rule types are available: |
| 40 | + |
| 41 | +| Rule type | Identifier | Description | Expression value | |
| 42 | +|---|---|---|---| |
| 43 | +| `Ibexa\Discounts\Value\DiscountRule\FixedAmount` | `fixed_amount` | Deducts the specified amount, for example 10 EUR, from the base price | `discount_amount` | |
| 44 | +| `Ibexa\Discounts\Value\DiscountRule\Percentage` | `percentage` | Deducts the specified percentage, for example -10%, from the base price | `discount_percentage` | |
| 45 | + |
| 46 | +Only a single discount can be applied to a given product, and a discount can only have a single rule. |
| 47 | + |
| 48 | +You can create your own rules by creating a class implementing the [DiscountRuleInterface](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Discounts-Value-DiscountRuleInterface.html) interface. |
| 49 | + |
| 50 | +### Conditions |
| 51 | + |
| 52 | +With conditions you can narrow down the scenarios in which the discount applies. The following conditions are available: |
| 53 | + |
| 54 | +| Condition | Applies to | Identifier | Description | Expression values | |
| 55 | +|---|---|---|---|---| |
| 56 | +| `Ibexa\Discounts\Value\DiscountCondition\IsInCategory` | Cart, Catalog | `is_in_category` | Checks if the product belongs to specified [product categories]([[= user_doc =]]/pim/work_with_product_categories) | `categories` | |
| 57 | +| `Ibexa\Discounts\Value\DiscountCondition\IsInCurrency` | Cart, Catalog |`is_in_currency` | Checks if the product has price in the specified currency | `currency_code` | |
| 58 | +| `Ibexa\Discounts\Value\DiscountCondition\IsInRegions` | Cart, Catalog | `is_in_regions` | Checks if the customer is making the purchase in one of the specified regions | `regions` | |
| 59 | +| `Ibexa\Discounts\Value\DiscountCondition\IsProductInArray` | Cart, Catalog| `is_product_in_array` | Checks if the product belongs to the group of selected products | `product_codes` | |
| 60 | +| `Ibexa\Discounts\Value\DiscountCondition\IsUserInCustomerGroup` | Cart, Catalog| `is_user_in_customer_group` | Check if the customer belongs to specified [customer groups](customer_groups.md) | `customer_groups` | |
| 61 | +| `Ibexa\Discounts\Value\DiscountCondition\IsProductInQuantityInCart` | Cart | `is_product_in_quantity_in_cart` | Checks if the required minimum quantity of a given product is present in the cart | `quantity` | |
| 62 | +| `Ibexa\Discounts\Value\DiscountCondition\MinimumPurchaseAmount` | Cart | `minimum_purchase_amount` | Checks if purchase amount in the cart exceeds the specified minimum | `minimum_purchase_amount` | |
| 63 | +| `Ibexa\DiscountsCodes\Value\DiscountCondition\IsValidDiscountCode` | Cart | `is_valid_discount_code` | Checks if the correct discount code has been provided and how many times it was used by the customer | `discount_code`, `usage_count` | |
| 64 | + |
| 65 | +When multiple conditions are specified, all of them must be met. |
| 66 | + |
| 67 | +You can create your own conditions by creating a class implementing the [DiscountConditionInterface](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Discounts-Value-DiscountConditionInterface.html) interface. |
| 68 | + |
| 69 | +### Priority |
| 70 | + |
| 71 | +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. |
| 72 | + |
| 73 | +### Start and end date |
| 74 | + |
| 75 | +Discounts can be permanent, or valid only in specified time frame. |
| 76 | + |
| 77 | +Every discount has a start date, which defaults to the date when the discount was created. |
| 78 | +The end date can be set to `null` to make the discount permanent. |
| 79 | + |
| 80 | +### Discount translations |
| 81 | + |
| 82 | +The discount has four properties that can be translated: |
| 83 | + |
| 84 | +| Property | Usage | |
| 85 | +|---|---| |
| 86 | +| Name | Internal information for store managers | |
| 87 | +| Description | Internal information for store managers | |
| 88 | +| Promotion label | Information displayed to customers | |
| 89 | +| Promotion description | Information displayed to customers | |
| 90 | + |
| 91 | +Use the [DiscountTranslationStruct](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Discounts-Value-Struct-DiscountTranslationStruct.html) to provide translations for discounts. |
| 92 | + |
| 93 | +### Discount codes |
| 94 | + |
| 95 | +To activate a cart discount only after a proper discount code is provided, you need to: |
| 96 | + |
| 97 | +1. Create a discount code using the [DiscountCodeServiceInterface::createDiscountCode()](/api/php_api/php_api_reference/classes/Ibexa-Contracts-DiscountsCodes-DiscountCodeServiceInterface.html#method_createDiscountCode) method |
| 98 | +1. Attach it to a discount by using the `IsValidDiscountCode` condition |
| 99 | + |
| 100 | +Set the [`usedLimit`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-DiscountsCodes-Value-Struct-DiscountCodeCreateStruct.html#method___construct) property to the number of times a single customer can use this code, or to `null` to make the usage unlimited. |
| 101 | + |
| 102 | +The [`DiscountCodeServiceInterface::registerUsage()`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-DiscountsCodes-DiscountCodeServiceInterface.html#method_registerUsage) method is used to track the number of times a discount code has been used. |
| 103 | + |
| 104 | +### Example API usage |
| 105 | + |
| 106 | +The example below contains a Command creating a cart discount. The discount: |
| 107 | + |
| 108 | +- has the highest possible priority value |
| 109 | +- deducts 10 EUR from the base price of the product |
| 110 | +- is permanent |
| 111 | +- is valid in Germany and France |
| 112 | +- applies to 2 products |
| 113 | +- requires a `summer10` discount code to be activated. The code can be used unlimited number of times |
| 114 | + |
| 115 | +``` php hl_lines="53-59 61-84" |
| 116 | +[[= include_file('code_samples/discounts/src/Command/ManageDiscountsCommand.php') =]] |
| 117 | +``` |
| 118 | + |
| 119 | +Similarly, use the `deleteDiscount`, `deleteTranslation`, `disableDiscount`, `enableDiscount`, and `updateDiscount` methods from the [DiscountServiceInterface](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Discounts-DiscountServiceInterface.html) to manage the discounts. You can always attach additional logic to the Discounts API by listening to the available events. |
| 120 | + |
| 121 | +## Search |
| 122 | + |
| 123 | +You can search for Discounts using the [`DiscountServiceInterface::findDiscounts()](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Discounts-DiscountServiceInterface.html#method_findDiscounts) method. |
| 124 | +To learn more about the available search options, see Discounts' Search Criteria and Sort Clauses. |
| 125 | + |
| 126 | +For discount codes, you can query the database for discount code usage using [`DiscountCodeServiceInterface::findCodeUsages()`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-DiscountsCodes-DiscountCodeServiceInterface.html#method_findCodeUsages) and [`DiscountCodeUsageQuery`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-DiscountsCodes-Value-Query-DiscountCodeUsageQuery.html). |
0 commit comments