Skip to content

Commit a0521aa

Browse files
authored
Merge pull request #138 from IrinaZhadzinets/issue-4353
#4353 - Add category in magento tags
2 parents 6974ea2 + ded1179 commit a0521aa

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/Model/Resolver/Products/Query/Search.php

+25-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Exception;
1111
use Magento\Catalog\Api\Data\ProductSearchResultsInterfaceFactory;
12+
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
1213
use Magento\CatalogGraphQl\DataProvider\Product\SearchCriteriaBuilder;
1314
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\ProductSearch;
1415
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
@@ -87,6 +88,11 @@ class Search extends CoreSearch
8788
*/
8889
protected $emulateSearchResult;
8990

91+
/**
92+
* @var CategoryCollectionFactory
93+
*/
94+
protected CategoryCollectionFactory $categoryCollectionFactory;
95+
9096
/**
9197
* @param SearchInterface $search
9298
* @param SearchResultFactory $searchResultFactory
@@ -99,6 +105,7 @@ class Search extends CoreSearch
99105
* @param DataPostProcessor $productPostProcessor
100106
* @param QueryFactory $queryFactory
101107
* @param StoreManagerInterface $storeManager
108+
* @param CategoryCollectionFactory $categoryCollectionFactory
102109
*/
103110
public function __construct(
104111
SearchInterface $search,
@@ -111,7 +118,8 @@ public function __construct(
111118
SearchCriteriaBuilder $searchCriteriaBuilder,
112119
DataPostProcessor $productPostProcessor,
113120
QueryFactory $queryFactory,
114-
StoreManagerInterface $storeManager
121+
StoreManagerInterface $storeManager,
122+
CategoryCollectionFactory $categoryCollectionFactory
115123
) {
116124
parent::__construct(
117125
$search,
@@ -133,6 +141,7 @@ public function __construct(
133141
$this->storeManager = $storeManager;
134142
$this->productSearchResultsInterfaceFactory = $productSearchResultsInterfaceFactory;
135143
$this->emulateSearchResult = $emulateSearchResult;
144+
$this->categoryCollectionFactory = $categoryCollectionFactory;
136145
}
137146

138147
/**
@@ -153,6 +162,21 @@ public function getResult(
153162
$searchCriteria = $this->buildSearchCriteria($args, $info);
154163
$itemsResults = $this->getSearchResults($searchCriteria, $info);
155164

165+
// When adding a new product through the admin panel, it does not appear
166+
// on the category page (without cleaning cache), if the category won`t
167+
// mentions in request in Magento Tags (and not just product Tags as now).
168+
//
169+
// To add the category to tags, need to cause category loading when receiving products for it
170+
// (only loading is enough because the tags are added to load_after)
171+
//
172+
// Related task: https://github.com/scandipwa/scandipwa/issues/4353
173+
if (!empty($args['filter']['category_id'])) {
174+
$this->categoryCollectionFactory->create()
175+
->addAttributeToSelect('entity_id')
176+
->addAttributeToFilter('entity_id', $args['filter']['category_id'])
177+
->load();
178+
}
179+
156180
if ($this->includeItems($info)) {
157181
// load product collection only if items are requested
158182
$searchResults = $this->productsProvider->getList(

0 commit comments

Comments
 (0)