-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2706 from magento-architects/MAGETWO-92773-Anchor…
…-Category [architects] MAGETWO-92773: [GraphQL] Products cannot be fetched in parent/anchor category #89
- Loading branch information
Showing
9 changed files
with
139 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...l/Resolver/Products/SearchCriteria/CollectionProcessor/FilterProcessor/CategoryFilter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CatalogGraphQl\Model\Resolver\Products\SearchCriteria\CollectionProcessor\FilterProcessor; | ||
|
||
use Magento\Catalog\Model\CategoryFactory; | ||
use Magento\Catalog\Model\ResourceModel\Category as CategoryResourceModel; | ||
use Magento\Catalog\Model\ResourceModel\Product\Collection; | ||
use Magento\Framework\Api\Filter; | ||
use Magento\Framework\Api\SearchCriteria\CollectionProcessor\FilterProcessor\CustomFilterInterface; | ||
use Magento\Framework\Data\Collection\AbstractDb; | ||
use Magento\Framework\Exception\LocalizedException; | ||
|
||
/** | ||
* Category filter allows to filter products collection using 'category_id' filter from search criteria. | ||
*/ | ||
class CategoryFilter implements CustomFilterInterface | ||
{ | ||
/** | ||
* @var CategoryFactory | ||
*/ | ||
private $categoryFactory; | ||
|
||
/** | ||
* @var CategoryResourceModel | ||
*/ | ||
private $categoryResourceModel; | ||
|
||
/** | ||
* @param CategoryFactory $categoryFactory | ||
* @param CategoryResourceModel $categoryResourceModel | ||
*/ | ||
public function __construct( | ||
CategoryFactory $categoryFactory, | ||
CategoryResourceModel $categoryResourceModel | ||
) { | ||
$this->categoryFactory = $categoryFactory; | ||
$this->categoryResourceModel = $categoryResourceModel; | ||
} | ||
|
||
/** | ||
* Apply filter by 'category_id' to product collection. | ||
* | ||
* For anchor categories, the products from all children categories will be present in the result. | ||
* | ||
* @param Filter $filter | ||
* @param AbstractDb $collection | ||
* @return bool Whether the filter is applied | ||
* @throws LocalizedException | ||
*/ | ||
public function apply(Filter $filter, AbstractDb $collection) | ||
{ | ||
$conditionType = $filter->getConditionType(); | ||
|
||
if ($conditionType !== 'eq') { | ||
throw new LocalizedException(__("'category_id' only supports 'eq' condition type.")); | ||
} | ||
|
||
$categoryId = $filter->getValue(); | ||
/** @var Collection $collection */ | ||
$category = $this->categoryFactory->create(); | ||
$this->categoryResourceModel->load($category, $categoryId); | ||
$collection->addCategoryFilter($category); | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters