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
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:
Create a class that implements the [`ProductAvailabilityStrategyInterface`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-ProductCatalog-ProductAvailabilityStrategyInterface.html) interface:
-`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`:
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):
See [Product Search Criteria](product_search_criteria.md) and [Product Sort Clauses](product_sort_clauses.md) references for more information about how to use the [`ProductQuery`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-ProductCatalog-Values-Product-ProductQuery.html) class.
@@ -41,21 +41,21 @@ See [Product Search Criteria](product_search_criteria.md) and [Product Sort Clau
41
41
To create, update and delete products, use the `LocalProductServiceInterface`.
To create a product, use `LocalProductServiceInterface::newProductCreateStruct()` to get a [`ProductCreateStruct`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-ProductCatalog-Local-Values-Product-ProductCreateStruct.html).
48
48
Provide the method with the product type object and the main language code.
49
49
You also need to set (at least) the code for the product and the required Field of the underlying content type, `name`:
@@ -214,17 +214,30 @@ You can also get a list of product types with `ProductTypeServiceInterface::find
214
214
215
215
## Product availability
216
216
217
-
Product availability is an object which defines whether a product is available, and if so, in what stock.
217
+
Product availability is an object which defines whether a product is set as available, in what stock, and whether it can be ordered.
218
218
To manage it, use [`ProductAvailabilityServiceInterface`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-ProductCatalog-ProductAvailabilityServiceInterface.html).
219
219
220
-
To check whether a product is available (with or without stock defined), use `ProductAvailabilityServiceInterface::hasAvailability()`.
220
+
The [`AvailabilityInterface`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-ProductCatalog-Values-Availability-AvailabilityInterface.html) provides two distinct availability values:
221
221
222
-
Get the availability object with `ProductAvailabilityServiceInterface::getAvailability()`.
223
-
You can then use `ProductAvailabilityServiceInterface::getStock()`to get the stock number for the product:
222
+
-`getAvailability()` returns the value of availability flag as set for the product
223
+
-`getComputedAvailability()`returns whether the product can be ordered
For more information about the distinction between these two values, see [Availability and computed availability](products.md#availability-and-computed-availability).
226
+
227
+
To check whether a product is set as available, use `ProductAvailabilityServiceInterface::hasAvailability()`.
228
+
229
+
You can get the availability object with `ProductAvailabilityServiceInterface::getAvailability()`.
230
+
The returned object contains both the stored and computed availability:
To evaluate computed availability for a [specific context](create_custom_availability_strategy.md), for example, a specific requested quantity or customer group, pass an optional [`AvailabilityContextInterface`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-ProductCatalog-Values-Availability-AvailabilityContextInterface.html) object as the second argument:
To change availability for a product, use `ProductAvailabilityServiceInterface::updateProductAvailability()` with a [`ProductAvailabilityUpdateStruct`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-ProductCatalog-Values-Availability-ProductAvailabilityUpdateStruct.html) and provide it with the product object.
Copy file name to clipboardExpand all lines: docs/product_catalog/products.md
+17-5Lines changed: 17 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -96,12 +96,24 @@ You set product availability per variant or per base product:
96
96
- if a product cannot have variants (has no attributes with the "Used for product variants" flag), you set availability per base product
97
97
- if a product can have variants (even if no variants are configured yet), you set availability per variant.
98
98
99
-
When a product is available, it can have numerical stock defined.
99
+
When a product is set as available, it can have numerical stock defined.
100
100
The stock can also be set to infinite (for example, in case of digital products).
101
101
102
-
!!! note
102
+
### Availability and computed availability
103
103
104
-
Availability doesn't automatically mean that a product can be ordered.
105
-
A product can be available, but have zero stock.
104
+
Setting a product as available doesn't automatically mean that it can be ordered.
105
+
For example, a product can be set as available, but have zero stock.
106
106
107
-
A product can only be ordered when it has either positive stock, or stock set to infinite.
107
+
The product catalog distinguishes between two types of availability:
108
+
109
+
- Availability as a value set per product or variant
110
+
111
+
Availability represents whether the product was set as **Available**, for example in the [back office **Availability** tab]([[= user_doc =]]/product_catalog/manage_availability_and_stock/#set-product-availability) or [PHP API](product_api.md#product-availability).
112
+
113
+
- Computed availability
114
+
115
+
Computed availability represents whether the product can actually be ordered.
116
+
By default, a product can only be ordered when it's set as available and has either positive or infinite stock.
117
+
118
+
You can implement a custom strategy to handle different selling scenarios, such as minimum order quantity, minimum stock quantity, or region-specific availability.
119
+
For more information, see [Create custom availability strategy](create_custom_availability_strategy.md).
The `ProductAvailability` Search Criterion searches for products by their availability.
7
+
The `ProductAvailability` Search Criterion searches for products by the availability flag, the boolean value set per product or variant.
8
+
9
+
To search for products that can be ordered, recreate the availability conditions with [existing product search criteria](product_search_criteria.md), for example [LogicalAnd](/api/php_api/php_api_reference/classes/Ibexa-Contracts-ProductCatalog-Values-Product-Query-Criterion-LogicalAnd.html), [LogicalOr](/api/php_api/php_api_reference/classes/Ibexa-Contracts-ProductCatalog-Values-Product-Query-Criterion-LogicalOr.html), and [`ProductStock`](productstock_criterion.md).
10
+
To recreate complex [custom availability strategies](create_custom_availability_strategy.md), you might need to implement [custom search criteria](search_criteria_and_sort_clauses.md#custom-criteria-and-sort-clauses) for the conditions not covered by the built-in ones.
11
+
12
+
For more information, see [Availability and computed availability](products.md#availability-and-computed-availability).
0 commit comments