From e247b0517d4e5652ed8fa58beeeb397b7166f01e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Noco=C5=84?= Date: Thu, 5 Feb 2026 17:14:46 +0100 Subject: [PATCH 1/2] Added doc for variant querying --- .../src/Command/ProductVariantCommand.php | 40 +++++++++++++++++-- docs/pim/product_api.md | 29 ++++++++++---- .../product_search_criteria.md | 5 ++- .../product_sort_clauses.md | 5 ++- phpstan-baseline.neon | 12 ++++++ 5 files changed, 78 insertions(+), 13 deletions(-) diff --git a/code_samples/api/product_catalog/src/Command/ProductVariantCommand.php b/code_samples/api/product_catalog/src/Command/ProductVariantCommand.php index 3b685a7fc4..6327f0cf9b 100644 --- a/code_samples/api/product_catalog/src/Command/ProductVariantCommand.php +++ b/code_samples/api/product_catalog/src/Command/ProductVariantCommand.php @@ -9,7 +9,9 @@ use Ibexa\Contracts\ProductCatalog\Local\LocalProductServiceInterface; use Ibexa\Contracts\ProductCatalog\Local\Values\Product\ProductVariantCreateStruct; use Ibexa\Contracts\ProductCatalog\ProductServiceInterface; +use Ibexa\Contracts\ProductCatalog\Values\Content\Query\Criterion\ProductCriterionAdapter; use Ibexa\Contracts\ProductCatalog\Values\Product\ProductVariantQuery; +use Ibexa\Contracts\ProductCatalog\Values\Product\Query\Criterion; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; @@ -46,12 +48,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int $productCode = $input->getArgument('productCode'); $product = $this->productService->getProduct($productCode); - // Get variants - $variantQuery = new ProductVariantQuery(0, 5); + // Get variants filtered by variant codes + $codeQuery = new ProductVariantQuery(); + $codeQuery->setVariantCodes(['DESK1', 'DESK2']); + $specificVariants = $this->productService->findProductVariants($product, $codeQuery)->getVariants(); - $variants = $this->productService->findProductVariants($product, $variantQuery)->getVariants(); + // Get variants with specific attributes + $combinedQuery = new ProductVariantQuery(); + $combinedQuery->setAttributesCriterion( + new ProductCriterionAdapter( + new Criterion\LogicalAnd([ + new Criterion\ColorAttribute('color', ['red', 'blue']), + new Criterion\IntegerAttribute('size', 42), + ]) + ) + ); + $filteredVariants = $this->productService->findProductVariants($product, $combinedQuery)->getVariants(); - foreach ($variants as $variant) { + foreach ($specificVariants as $variant) { $output->writeln($variant->getName()); $attributes = $variant->getDiscriminatorAttributes(); foreach ($attributes as $attribute) { @@ -67,6 +81,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int $this->localProductService->createProductVariants($product, $variantCreateStructs); + // Search variants across all products + $query = new ProductVariantQuery(); + $query->setVariantCodes(['DESK1', 'DESK2']); + $variantList = $this->productService->findVariants($query); + + foreach ($variantList->getVariants() as $variant) { + $output->writeln($variant->getName()); + } + + // Search variants with attribute criterion + $colorQuery = new ProductVariantQuery(); + $colorQuery->setAttributesCriterion( + new ProductCriterionAdapter( + new Criterion\ColorAttribute('color', ['red']) + ) + ); + $redVariants = $this->productService->findVariants($colorQuery); + return self::SUCCESS; } } diff --git a/docs/pim/product_api.md b/docs/pim/product_api.md index dad5a2e096..fb8f5b81ff 100644 --- a/docs/pim/product_api.md +++ b/docs/pim/product_api.md @@ -58,20 +58,35 @@ To delete a product, use `LocalProductServiceInterface::deleteProduct()`: ### Product variants -You can access the variants of a product by using `ProductServiceInterface::findProductVariants()`. +You can access the variants of a product by using [`ProductServiceInterface::findProductVariants()`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-ProductCatalog-ProductServiceInterface.html#method_findProductVariants). The method takes the product object and a [`ProductVariantQuery`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-ProductCatalog-Values-Product-ProductVariantQuery.html) object as parameters. -A `ProductVariantQuery` lets you define the offset and limit of the variant query. -The default offset is 0, and limit is 25. +You can filter variants by variant codes or use product criteria: ``` php -[[= include_file('code_samples/api/product_catalog/src/Command/ProductVariantCommand.php', 49, 52) =]] +[[= include_file('code_samples/api/product_catalog/src/Command/ProductVariantCommand.php', 50, 66) =]] ``` -From a variant ([`ProductVariantInterface`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-ProductCatalog-Values-ProductVariantInterface.html)), you can access the attributes that are used to generate the variant by using `ProductVariantInterface::getDiscriminatorAttributes()`. +From a variant ([`ProductVariantInterface`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-ProductCatalog-Values-ProductVariantInterface.html)), you can access the attributes that are used to generate the variant by using [`ProductVariantInterface::getDiscriminatorAttributes()`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-ProductCatalog-Values-ProductVariantInterface.html#method_getDiscriminatorAttributes). ``` php -[[= include_file('code_samples/api/product_catalog/src/Command/ProductVariantCommand.php', 53, 60) =]] +[[= include_file('code_samples/api/product_catalog/src/Command/ProductVariantCommand.php', 69, 73) =]] +``` + +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 [`ProductVariantQuery`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-ProductCatalog-Values-Product-ProductVariantQuery.html) class. + +#### Searching variants across products + +To search for variants across all products, use [`ProductServiceInterface::findVariants()`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-ProductCatalog-ProductServiceInterface.html#method_findVariants) +This method takes a [`ProductVariantQuery`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-ProductCatalog-Values-Product-ProductVariantQuery.html) object and returns variants regardless of their base product. + +Unlike `findProductVariants()`, which requires a specific product object, `findVariants()` allows you to search the entire variant catalog. + +You can filter variants using Product Criteria wrapped in a [`ProductCriterionAdapter`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-ProductCatalog-Values-Content-Query-Criterion-ProductCriterionAdapter.html). +For example, search for variants with specific product codes or attribute values: + +``` php +[[= include_file('code_samples/api/product_catalog/src/Command/ProductVariantCommand.php', 83, 100) =]] ``` #### Creating variants @@ -81,7 +96,7 @@ This method takes the product and an array of [`ProductVariantCreateStruct`](/ap `ProductVariantCreateStruct` specifies the attribute values and the code for the new variant. ``` php -[[= include_file('code_samples/api/product_catalog/src/Command/ProductVariantCommand.php', 62, 68) =]] +[[= include_file('code_samples/api/product_catalog/src/Command/ProductVariantCommand.php', 85, 91) =]] ``` ### Product assets diff --git a/docs/search/criteria_reference/product_search_criteria.md b/docs/search/criteria_reference/product_search_criteria.md index 35a3948ac3..907c5a0e4f 100644 --- a/docs/search/criteria_reference/product_search_criteria.md +++ b/docs/search/criteria_reference/product_search_criteria.md @@ -5,10 +5,13 @@ page_type: reference # Product Search Criteria reference -Product Search Criteria are only supported by [Product Search (`ProductServiceInterface::findProduct`)](product_api.md#products). +Product Search Criteria are supported by [Product Search (`ProductServiceInterface::findProduct`)](product_api.md#products). Search Criterion let you filter product by specific attributes, for example, color, availability, or price. +Product Search Criteria can also be used with `ProductVariantQuery` when wrapped in a [`ProductCriterionAdapter`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-ProductCatalog-Values-Content-Query-Criterion-ProductCriterionAdapter.html) +. See the [product variant search examples](product_api.md#searching-variants-across-products) for more information. + ## Product Search Criteria |Search Criterion|Search based on| diff --git a/docs/search/sort_clause_reference/product_sort_clauses.md b/docs/search/sort_clause_reference/product_sort_clauses.md index 77aebe252b..1e3e1ce3d6 100644 --- a/docs/search/sort_clause_reference/product_sort_clauses.md +++ b/docs/search/sort_clause_reference/product_sort_clauses.md @@ -5,10 +5,13 @@ page_type: reference # Product Sort Clauses -Product Sort Clauses are only supported by [Product Search (`ProductServiceInterface::findProduct`)](product_api.md#products). +Product Sort Clauses are supported by [Product Search (`ProductServiceInterface::findProduct`)](product_api.md#products). By using Sort Clause you can filter product by specific attributes, for example: price, code, or availability. +Product Sort Clauses can also be used with `ProductVariantQuery` when wrapped in a [`ProductCriterionAdapter`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-ProductCatalog-Values-Content-Query-Criterion-ProductCriterionAdapter.html) +. See the [product variant search examples](../../pim/product_api.md#searching-variants-across-products) for more information. + | Sort Clause | Sorting based on | |-----|-----| |[BasePrice](baseprice_sort_clause.md)|Base product price| diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index f0376d016f..15279505d8 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -42,6 +42,18 @@ parameters: count: 1 path: code_samples/api/product_catalog/src/Command/ProductPriceCommand.php + - + message: '#^Parameter \#1 \$criterion of method Ibexa\\Contracts\\ProductCatalog\\Values\\Product\\ProductVariantQuery\:\:setAttributesCriterion\(\) expects Ibexa\\Contracts\\ProductCatalog\\Values\\Content\\Query\\Criterion\\ProductCriterionAdapter\\|null, Ibexa\\Contracts\\ProductCatalog\\Values\\Content\\Query\\Criterion\\ProductCriterionAdapter\ given\.$#' + identifier: argument.type + count: 1 + path: code_samples/api/product_catalog/src/Command/ProductVariantCommand.php + + - + message: '#^Parameter \#1 \$criterion of method Ibexa\\Contracts\\ProductCatalog\\Values\\Product\\ProductVariantQuery\:\:setAttributesCriterion\(\) expects Ibexa\\Contracts\\ProductCatalog\\Values\\Content\\Query\\Criterion\\ProductCriterionAdapter\\|null, Ibexa\\Contracts\\ProductCatalog\\Values\\Content\\Query\\Criterion\\ProductCriterionAdapter\ given\.$#' + identifier: argument.type + count: 1 + path: code_samples/api/product_catalog/src/Command/ProductVariantCommand.php + - message: '#^Cannot call method getDateTime\(\) on Ibexa\\Contracts\\Calendar\\Event\|null\.$#' identifier: method.nonObject From 580dd913430b8fc66691e58f639139146e6ca299 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Noco=C5=84?= Date: Fri, 6 Feb 2026 09:42:34 +0100 Subject: [PATCH 2/2] Added release notes stub --- docs/release_notes/ibexa_dxp_v5.0.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/release_notes/ibexa_dxp_v5.0.md b/docs/release_notes/ibexa_dxp_v5.0.md index 074b312180..f9ab2d2e5f 100644 --- a/docs/release_notes/ibexa_dxp_v5.0.md +++ b/docs/release_notes/ibexa_dxp_v5.0.md @@ -10,6 +10,15 @@ month_change: false
+ +#### Improved product variant querying + +Product variant querying now supports filtering by variant codes and product attribute criteria. + +You can now use [`ProductServiceInterface::findVariants()`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-ProductCatalog-ProductServiceInterface.html#method_findVariants) to search for variants across all products, regardless of their base product. + +For more information, see [Product API - Searching variants](product_api.md#searching-variants-across-products). + [[% set version = 'v5.0.5' %]] [[= release_note_entry_begin("Ibexa DXP " + version, '2026-01-15', ['Headless', 'Experience', 'Commerce']) =]]