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/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