|
| 1 | +--- |
| 2 | +description: Implement custom availability strategies to handle different business scenarios, for example pre-orders or per-region availability. |
| 3 | +--- |
| 4 | + |
| 5 | +# Create custom availability strategy |
| 6 | + |
| 7 | +The product catalog uses an availability strategy to calculate [computed availability](products.md#availability-and-computed-availability) for a product. |
| 8 | +Computed availability decides whether the customers can order the product. |
| 9 | +The default availability strategy is based on the product availability and stock amount. |
| 10 | + |
| 11 | +You can replace this logic with a custom strategy to handle specific business scenarios, for example preorders, minimum order quantities, or per-region availability. |
| 12 | + |
| 13 | +The following example implements an availability strategy which allows buying products when they're set as available, without taking their stock into account. |
| 14 | +You could use it for [virtual products](products.md#product-types) or in preorder scenarios. |
| 15 | + |
| 16 | +## Create custom availability context |
| 17 | + |
| 18 | +Use an availability context to pass the parameters needed by the strategy to evaluate computed availability. |
| 19 | +To do it, create a class that implements the [`AvailabilityContextInterface`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-ProductCatalog-Values-Availability-AvailabilityContextInterface.html) interface: |
| 20 | + |
| 21 | +``` php |
| 22 | +[[= include_file('code_samples/pim/availability/src/PurchasableWithoutStockAvailabilityContext.php') =]] |
| 23 | +``` |
| 24 | + |
| 25 | +## Create custom availability strategy |
| 26 | + |
| 27 | +Create a class that implements the [`ProductAvailabilityStrategyInterface`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-ProductCatalog-ProductAvailabilityStrategyInterface.html) interface: |
| 28 | + |
| 29 | +``` php |
| 30 | +[[= include_file('code_samples/pim/availability/src/ProductAvailabilityPurchasableWithoutStockStrategy.php') =]] |
| 31 | +``` |
| 32 | + |
| 33 | +The strategy has two methods: |
| 34 | + |
| 35 | +- `accept()` decides if the strategy can handle the provided availability context |
| 36 | +- `getProductAvailability()` returns an [`AvailabilityInterface`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-ProductCatalog-Values-Availability-AvailabilityInterface.html) object |
| 37 | + |
| 38 | +When constructing the `AvailabilityInterface` object, provide the stock amount, the availability flag, and the result of your custom availability logic. |
| 39 | + |
| 40 | +## Register strategy as a service |
| 41 | + |
| 42 | +If you're not using [autowiring]([[= symfony_doc =]]/service_container/autowiring.html), tag the strategy service with `ibexa.product_catalog.availability.strategy`: |
| 43 | + |
| 44 | +``` yaml |
| 45 | +[[= include_file('code_samples/pim/availability/config/custom_services.yaml') =]] |
| 46 | +``` |
| 47 | + |
| 48 | +## Use custom context |
| 49 | + |
| 50 | +To evaluate product availability using a custom strategy, pass the custom context as the second argument to [`ProductAvailabilityServiceInterface::getAvailability()`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-ProductCatalog-ProductAvailabilityServiceInterface.html): |
| 51 | + |
| 52 | +```php |
| 53 | +[[= include_code('code_samples/api/product_catalog/src/Command/ProductCommand.php', 122, 127, remove_indent=True) =]] |
| 54 | +``` |
0 commit comments