|
| 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 when parsing REST API responses. |
| 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` | <nobr>`fixed_amount`</nobr> | Deducts the specified amount, for example <nobr>10 EUR</nobr>, from the base price | <nobr>`discount_amount`</nobr> | |
| 44 | +| `Ibexa\Discounts\Value\DiscountRule\Percentage` | <nobr>`percentage`</nobr> | Deducts the specified percentage, for example -10%, from the base price | <nobr>`discount_percentage`</nobr> | |
| 45 | + |
| 46 | +Only a single discount can be applied to a given product, and a discount can only have a single rule. |
| 47 | + |
| 48 | +### Conditions |
| 49 | + |
| 50 | +With conditions you can narrow down the scenarios in which the discount applies. The following conditions are available: |
| 51 | + |
| 52 | +| Condition | Applies to | Identifier | Description | Expression values | |
| 53 | +|---|---|---|---|---| |
| 54 | +| `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` | |
| 55 | +| `Ibexa\Discounts\Value\DiscountCondition\IsInCurrency` | Cart, Catalog |`is_in_currency` | Checks if the product has price in the specified currency | `currency_code` | |
| 56 | +| `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` | |
| 57 | +| `Ibexa\Discounts\Value\DiscountCondition\IsProductInArray` | Cart, Catalog| `is_product_in_array` | Checks if the product belongs to the group of selected products | `product_codes` | |
| 58 | +| `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` | |
| 59 | +| `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` | |
| 60 | +| `Ibexa\Discounts\Value\DiscountCondition\MinimumPurchaseAmount` | Cart | `minimum_purchase_amount` | Checks if purchase amount in the cart exceeds the specified minimum | `minimum_purchase_amount` | |
| 61 | +| `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` | |
| 62 | + |
| 63 | +When multiple conditions are specified, all of them must be met. |
| 64 | + |
| 65 | +### Priority |
| 66 | + |
| 67 | +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. |
| 68 | + |
| 69 | +### Start and end date |
| 70 | + |
| 71 | +Discounts can be permanent, or valid only in a specified time frame. |
| 72 | + |
| 73 | +Every discount has a start date, which defaults to the date when the discount was created. |
| 74 | +The end date can be set to `null` to make the discount permanent. |
| 75 | + |
| 76 | +### Status |
| 77 | + |
| 78 | +You can disable a discount anytime to stop it from being active, even if the conditions enforced by start and end date are met. |
| 79 | + |
| 80 | +Only disabled discounts can be deleted. |
| 81 | + |
| 82 | +### Discount translations |
| 83 | + |
| 84 | +The discount has four properties that can be translated: |
| 85 | + |
| 86 | +| Property | Usage | |
| 87 | +|---|---| |
| 88 | +| Name | Internal information for store managers | |
| 89 | +| Description | Internal information for store managers | |
| 90 | +| Promotion label | Information displayed to customers | |
| 91 | +| Promotion description | Information displayed to customers | |
| 92 | + |
| 93 | +Use the [`DiscountTranslationStruct`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Discounts-Value-Struct-DiscountTranslationStruct.html) to provide translations for discounts. |
| 94 | + |
| 95 | +### Discount codes |
| 96 | + |
| 97 | +To activate a cart discount only after a proper discount code is provided, you need to: |
| 98 | + |
| 99 | +1. Create a discount code using the [`DiscountCodeServiceInterface::createDiscountCode()`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-DiscountsCodes-DiscountCodeServiceInterface.html#method_createDiscountCode) method |
| 100 | +1. Attach it to a discount by using the `IsValidDiscountCode` condition |
| 101 | + |
| 102 | +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. |
| 103 | + |
| 104 | +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. |
| 105 | + |
| 106 | +### Example API usage |
| 107 | + |
| 108 | +The example below contains a Command creating a cart discount. The discount: |
| 109 | + |
| 110 | +- has the highest possible [priority](#priority) value |
| 111 | +- [rule](#rules) deducts 10 EUR from the base price of the product |
| 112 | +- is [permanent](#start-and-end-date) |
| 113 | +- [depends](#conditions) on |
| 114 | + - being bought from Germany or France |
| 115 | + - 2 products |
| 116 | + - a `summer10` [discount code](#discount-codes) which can be used unlimited number of times |
| 117 | + |
| 118 | +``` php hl_lines="60-66 68-92" |
| 119 | +[[= include_file('code_samples/discounts/src/Command/ManageDiscountsCommand.php') =]] |
| 120 | +``` |
| 121 | + |
| 122 | +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](discounts_events.md). |
| 123 | + |
| 124 | +## Search |
| 125 | + |
| 126 | +You can search for Discounts using the [`DiscountServiceInterface::findDiscounts()`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Discounts-DiscountServiceInterface.html#method_findDiscounts) method. |
| 127 | +To learn more about the available search options, see Discounts' [Search Criteria](discounts_criteria.md) and [Sort Clauses](discounts_sort_clauses.md). |
| 128 | + |
| 129 | +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