Skip to content

Commit

Permalink
Merge pull request #138 from IrinaZhadzinets/issue-4353
Browse files Browse the repository at this point in the history
#4353 - Add category in magento tags
  • Loading branch information
carinadues authored May 12, 2022
2 parents 6974ea2 + ded1179 commit a0521aa
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/Model/Resolver/Products/Query/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Exception;
use Magento\Catalog\Api\Data\ProductSearchResultsInterfaceFactory;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
use Magento\CatalogGraphQl\DataProvider\Product\SearchCriteriaBuilder;
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\ProductSearch;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
Expand Down Expand Up @@ -87,6 +88,11 @@ class Search extends CoreSearch
*/
protected $emulateSearchResult;

/**
* @var CategoryCollectionFactory
*/
protected CategoryCollectionFactory $categoryCollectionFactory;

/**
* @param SearchInterface $search
* @param SearchResultFactory $searchResultFactory
Expand All @@ -99,6 +105,7 @@ class Search extends CoreSearch
* @param DataPostProcessor $productPostProcessor
* @param QueryFactory $queryFactory
* @param StoreManagerInterface $storeManager
* @param CategoryCollectionFactory $categoryCollectionFactory
*/
public function __construct(
SearchInterface $search,
Expand All @@ -111,7 +118,8 @@ public function __construct(
SearchCriteriaBuilder $searchCriteriaBuilder,
DataPostProcessor $productPostProcessor,
QueryFactory $queryFactory,
StoreManagerInterface $storeManager
StoreManagerInterface $storeManager,
CategoryCollectionFactory $categoryCollectionFactory
) {
parent::__construct(
$search,
Expand All @@ -133,6 +141,7 @@ public function __construct(
$this->storeManager = $storeManager;
$this->productSearchResultsInterfaceFactory = $productSearchResultsInterfaceFactory;
$this->emulateSearchResult = $emulateSearchResult;
$this->categoryCollectionFactory = $categoryCollectionFactory;
}

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

// When adding a new product through the admin panel, it does not appear
// on the category page (without cleaning cache), if the category won`t
// mentions in request in Magento Tags (and not just product Tags as now).
//
// To add the category to tags, need to cause category loading when receiving products for it
// (only loading is enough because the tags are added to load_after)
//
// Related task: https://github.com/scandipwa/scandipwa/issues/4353
if (!empty($args['filter']['category_id'])) {
$this->categoryCollectionFactory->create()
->addAttributeToSelect('entity_id')
->addAttributeToFilter('entity_id', $args['filter']['category_id'])
->load();
}

if ($this->includeItems($info)) {
// load product collection only if items are requested
$searchResults = $this->productsProvider->getList(
Expand Down

0 comments on commit a0521aa

Please sign in to comment.