7
7
8
8
namespace Magento \CatalogGraphQl \DataProvider \Product \LayeredNavigation \Builder ;
9
9
10
+ use Magento \Catalog \Model \ResourceModel \Category \CollectionFactory ;
10
11
use Magento \CatalogGraphQl \DataProvider \CategoryAttributesMapper ;
11
12
use Magento \CatalogGraphQl \DataProvider \Category \Query \CategoryAttributeQuery ;
12
13
use Magento \CatalogGraphQl \DataProvider \Product \LayeredNavigation \LayerBuilderInterface ;
@@ -63,6 +64,11 @@ class Category implements LayerBuilderInterface
63
64
*/
64
65
private $ layerFormatter ;
65
66
67
+ /**
68
+ * @var CollectionFactory
69
+ */
70
+ private $ categoryCollectionFactory ;
71
+
66
72
/**
67
73
* @var Aggregations\Category\IncludeDirectChildrenOnly
68
74
*/
@@ -75,21 +81,24 @@ class Category implements LayerBuilderInterface
75
81
* @param ResourceConnection $resourceConnection
76
82
* @param LayerFormatter $layerFormatter
77
83
* @param Aggregations\Category\IncludeDirectChildrenOnly $includeDirectChildrenOnly
84
+ * @param CollectionFactory $categoryCollectionFactory
78
85
*/
79
86
public function __construct (
80
87
CategoryAttributeQuery $ categoryAttributeQuery ,
81
88
CategoryAttributesMapper $ attributesMapper ,
82
89
RootCategoryProvider $ rootCategoryProvider ,
83
90
ResourceConnection $ resourceConnection ,
84
91
LayerFormatter $ layerFormatter ,
85
- Aggregations \Category \IncludeDirectChildrenOnly $ includeDirectChildrenOnly
92
+ Aggregations \Category \IncludeDirectChildrenOnly $ includeDirectChildrenOnly ,
93
+ CollectionFactory $ categoryCollectionFactory
86
94
) {
87
95
$ this ->categoryAttributeQuery = $ categoryAttributeQuery ;
88
96
$ this ->attributesMapper = $ attributesMapper ;
89
97
$ this ->resourceConnection = $ resourceConnection ;
90
98
$ this ->rootCategoryProvider = $ rootCategoryProvider ;
91
99
$ this ->layerFormatter = $ layerFormatter ;
92
100
$ this ->includeDirectChildrenOnly = $ includeDirectChildrenOnly ;
101
+ $ this ->categoryCollectionFactory = $ categoryCollectionFactory ;
93
102
}
94
103
95
104
/**
@@ -112,6 +121,11 @@ function (AggregationValueInterface $value) {
112
121
$ bucket ->getValues ()
113
122
);
114
123
124
+ if ($ storeId ) {
125
+ $ storeFilteredCategoryIds = $ this ->getStoreCategoryIds ($ storeId );
126
+ $ categoryIds = \array_intersect ($ categoryIds , $ storeFilteredCategoryIds );
127
+ }
128
+
115
129
$ categoryIds = \array_diff ($ categoryIds , [$ this ->rootCategoryProvider ->getRootCategory ($ storeId )]);
116
130
$ categoryLabels = \array_column (
117
131
$ this ->attributesMapper ->getAttributesValues (
@@ -158,4 +172,25 @@ private function isBucketEmpty(?BucketInterface $bucket): bool
158
172
{
159
173
return null === $ bucket || !$ bucket ->getValues ();
160
174
}
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
+ }
161
196
}
0 commit comments