Skip to content

Commit e899be7

Browse files
author
Oleksandr Iegorov
committed
ACP2E-470: Products Aggregate for GraphQL returning data for all stores
1 parent 44a86cb commit e899be7

File tree

1 file changed

+36
-1
lines changed
  • app/code/Magento/CatalogGraphQl/DataProvider/Product/LayeredNavigation/Builder

1 file changed

+36
-1
lines changed

app/code/Magento/CatalogGraphQl/DataProvider/Product/LayeredNavigation/Builder/Category.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\CatalogGraphQl\DataProvider\Product\LayeredNavigation\Builder;
99

10+
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
1011
use Magento\CatalogGraphQl\DataProvider\CategoryAttributesMapper;
1112
use Magento\CatalogGraphQl\DataProvider\Category\Query\CategoryAttributeQuery;
1213
use Magento\CatalogGraphQl\DataProvider\Product\LayeredNavigation\LayerBuilderInterface;
@@ -63,6 +64,11 @@ class Category implements LayerBuilderInterface
6364
*/
6465
private $layerFormatter;
6566

67+
/**
68+
* @var CollectionFactory
69+
*/
70+
private $categoryCollectionFactory;
71+
6672
/**
6773
* @var Aggregations\Category\IncludeDirectChildrenOnly
6874
*/
@@ -75,21 +81,24 @@ class Category implements LayerBuilderInterface
7581
* @param ResourceConnection $resourceConnection
7682
* @param LayerFormatter $layerFormatter
7783
* @param Aggregations\Category\IncludeDirectChildrenOnly $includeDirectChildrenOnly
84+
* @param CollectionFactory $categoryCollectionFactory
7885
*/
7986
public function __construct(
8087
CategoryAttributeQuery $categoryAttributeQuery,
8188
CategoryAttributesMapper $attributesMapper,
8289
RootCategoryProvider $rootCategoryProvider,
8390
ResourceConnection $resourceConnection,
8491
LayerFormatter $layerFormatter,
85-
Aggregations\Category\IncludeDirectChildrenOnly $includeDirectChildrenOnly
92+
Aggregations\Category\IncludeDirectChildrenOnly $includeDirectChildrenOnly,
93+
CollectionFactory $categoryCollectionFactory
8694
) {
8795
$this->categoryAttributeQuery = $categoryAttributeQuery;
8896
$this->attributesMapper = $attributesMapper;
8997
$this->resourceConnection = $resourceConnection;
9098
$this->rootCategoryProvider = $rootCategoryProvider;
9199
$this->layerFormatter = $layerFormatter;
92100
$this->includeDirectChildrenOnly = $includeDirectChildrenOnly;
101+
$this->categoryCollectionFactory = $categoryCollectionFactory;
93102
}
94103

95104
/**
@@ -112,6 +121,11 @@ function (AggregationValueInterface $value) {
112121
$bucket->getValues()
113122
);
114123

124+
if ($storeId) {
125+
$storeFilteredCategoryIds = $this->getStoreCategoryIds($storeId);
126+
$categoryIds = \array_intersect($categoryIds, $storeFilteredCategoryIds);
127+
}
128+
115129
$categoryIds = \array_diff($categoryIds, [$this->rootCategoryProvider->getRootCategory($storeId)]);
116130
$categoryLabels = \array_column(
117131
$this->attributesMapper->getAttributesValues(
@@ -158,4 +172,25 @@ private function isBucketEmpty(?BucketInterface $bucket): bool
158172
{
159173
return null === $bucket || !$bucket->getValues();
160174
}
175+
176+
/**
177+
* List of store categories
178+
*
179+
* @param int $storeId
180+
* @return array
181+
*/
182+
private function getStoreCategoryIds(int $storeId): array
183+
{
184+
$storeRootCategoryId = $this->rootCategoryProvider->getRootCategory($storeId);
185+
$collection = $this->categoryCollectionFactory->create();
186+
$select = $collection->getSelect();
187+
$connection = $collection->getConnection();
188+
$select->where(
189+
$connection->quoteInto(
190+
'e.path LIKE ? OR e.entity_id=' . $connection->quote($storeRootCategoryId, 'int'),
191+
'%/' . $storeRootCategoryId . '/%'
192+
)
193+
);
194+
return $collection->getAllIds();
195+
}
161196
}

0 commit comments

Comments
 (0)