Skip to content

Commit 33219c8

Browse files
32460 Filter by multiple categories url paths
1 parent 8cea512 commit 33219c8

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Products/Query/CategoryUrlPathArgsProcessor.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,22 @@ public function process(
6666
/** @var Collection $collection */
6767
$collection = $this->collectionFactory->create();
6868
$collection->addAttributeToSelect('entity_id');
69-
$collection->addAttributeToFilter('url_path', $pathFilter['eq']);
69+
$collection->addAttributeToFilter('url_path', $pathFilter);
7070

7171
if ($collection->count() === 0) {
7272
throw new GraphQlInputException(
7373
__('No category with the provided `%1` was found', [self::URL_PATH])
7474
);
75+
} elseif ($collection->count() === 1) {
76+
$category = $collection->getFirstItem();
77+
$args['filter'][self::ID]['eq'] = $category->getId();
78+
} else {
79+
$categoryIds = [];
80+
foreach ($collection as $category) {
81+
$categoryIds[] = $category->getId();
82+
}
83+
$args['filter'][self::ID]['in'] = $categoryIds;
7584
}
76-
$category = $collection->getFirstItem();
77-
$args['filter'][self::ID]['eq'] = $category->getId();
7885

7986
unset($args['filter'][self::URL_PATH]);
8087
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/ProductSearchTest.php

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,60 @@ public function testFilterByCategoryUrlPath()
146146
}
147147
}
148148

149+
/**
150+
* Filter by multiple categories url paths
151+
*
152+
* @magentoApiDataFixture Magento/Catalog/_files/categories.php
153+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
154+
*/
155+
public function testFilterByMultipleCategoriesUrlPaths()
156+
{
157+
$categoriesPath = ['category-1/category-1-2','category-1/category-1-1'];
158+
159+
$query = <<<QUERY
160+
{
161+
products(filter:{
162+
category_url_path : {in:["{$categoriesPath[0]}","{$categoriesPath[1]}"]}
163+
}) {
164+
total_count
165+
items {
166+
name
167+
sku
168+
}
169+
}
170+
}
171+
QUERY;
172+
$response = $this->graphQlQuery($query);
173+
$this->assertEquals(3, $response['products']['total_count']);
174+
/** @var ProductRepositoryInterface $productRepository */
175+
$productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
176+
$product1 = $productRepository->get('simple');
177+
$product2 = $productRepository->get('12345');
178+
$product3 = $productRepository->get('simple-4');
179+
$filteredProducts = [$product3, $product2, $product1];
180+
$productItemsInResponse = array_map(null, $response['products']['items'], $filteredProducts);
181+
//phpcs:ignore Generic.CodeAnalysis.ForLoopWithTestFunctionCall
182+
for ($itemIndex = 0; $itemIndex < count($filteredProducts); $itemIndex++) {
183+
$this->assertNotEmpty($productItemsInResponse[$itemIndex]);
184+
//validate that correct products are returned
185+
$this->assertResponseFields(
186+
$productItemsInResponse[$itemIndex][0],
187+
[
188+
'name' => $filteredProducts[$itemIndex]->getName(),
189+
'sku' => $filteredProducts[$itemIndex]->getSku()
190+
]
191+
);
192+
}
193+
}
194+
149195
/**
150196
* Filter by wrong category url path
151197
*
152198
* @magentoApiDataFixture Magento/Catalog/_files/categories.php
153199
*/
154200
public function testFilterByWrongCategoryUrlPath()
155201
{
156-
$categoryUrlPath = 'test';
202+
$categoryUrlPath = 'not-a-category url path';
157203
$this->expectException(\Exception::class);
158204
$this->expectExceptionMessage('No category with the provided `category_url_path` was found');
159205

0 commit comments

Comments
 (0)