88namespace Magento \CatalogGraphQl \DataProvider \Product \LayeredNavigation \Builder \Aggregations \Category ;
99
1010use Magento \Catalog \Api \CategoryListInterface ;
11+ use Magento \Catalog \Model \Config \LayerCategoryConfig ;
1112use Magento \Framework \Api \SearchCriteriaBuilder ;
13+ use Magento \Framework \App \ObjectManager ;
1214use Magento \Framework \ObjectManager \ResetAfterRequestInterface ;
1315use Magento \Framework \Search \Response \Aggregation ;
1416use Magento \Framework \Search \Response \AggregationFactory ;
@@ -61,25 +63,34 @@ class IncludeDirectChildrenOnly implements ResetAfterRequestInterface
6163 */
6264 private $ searchCriteriaBuilder ;
6365
66+ /**
67+ * @var LayerCategoryConfig|null
68+ */
69+ private $ layerCategoryConfig ;
70+
6471 /**
6572 * @param AggregationFactory $aggregationFactory
6673 * @param BucketFactory $bucketFactory
6774 * @param StoreManagerInterface $storeManager
6875 * @param CategoryListInterface $categoryList
6976 * @param SearchCriteriaBuilder $searchCriteriaBuilder
77+ * @param LayerCategoryConfig|null $layerCategoryConfig
7078 */
7179 public function __construct (
7280 AggregationFactory $ aggregationFactory ,
7381 BucketFactory $ bucketFactory ,
7482 StoreManagerInterface $ storeManager ,
7583 CategoryListInterface $ categoryList ,
76- SearchCriteriaBuilder $ searchCriteriaBuilder
84+ SearchCriteriaBuilder $ searchCriteriaBuilder ,
85+ ?LayerCategoryConfig $ layerCategoryConfig = null
7786 ) {
7887 $ this ->aggregationFactory = $ aggregationFactory ;
7988 $ this ->bucketFactory = $ bucketFactory ;
8089 $ this ->storeManager = $ storeManager ;
8190 $ this ->categoryList = $ categoryList ;
8291 $ this ->searchCriteriaBuilder = $ searchCriteriaBuilder ;
92+ $ this ->layerCategoryConfig = $ layerCategoryConfig ?? ObjectManager::getInstance ()
93+ ->get (LayerCategoryConfig::class);
8394 }
8495
8596 /**
@@ -91,28 +102,34 @@ public function __construct(
91102 */
92103 public function filter (AggregationInterface $ aggregation , ?int $ storeId ): Aggregation
93104 {
94- $ categoryIdsRequested = $ this ->filter ['category ' ] ?? null ;
95- if ($ categoryIdsRequested === null ) {
96- return $ aggregation ;
97- }
98- $ buckets = $ aggregation ->getBuckets ();
99- $ categoryBucket = $ buckets [self ::CATEGORY_BUCKET ] ?? null ;
100- if ($ categoryBucket === null || empty ($ categoryBucket ->getValues ())) {
101- return $ aggregation ;
105+ if (!$ this ->layerCategoryConfig ->isCategoryFilterVisibleInLayerNavigation ()) {
106+ $ buckets = $ aggregation ->getBuckets ();
107+ unset($ buckets [self ::CATEGORY_BUCKET ]);
108+ } else {
109+ $ categoryIdsRequested = $ this ->filter ['category ' ] ?? null ;
110+ if ($ categoryIdsRequested === null ) {
111+ return $ aggregation ;
112+ }
113+ $ buckets = $ aggregation ->getBuckets ();
114+ $ categoryBucket = $ buckets [self ::CATEGORY_BUCKET ] ?? null ;
115+ if ($ categoryBucket === null || empty ($ categoryBucket ->getValues ())) {
116+ return $ aggregation ;
117+ }
118+ $ categoryIdsRequested = is_array ($ categoryIdsRequested ) ? $ categoryIdsRequested : [$ categoryIdsRequested ];
119+ $ bucketValuesFiltered = $ this ->filterBucketValues (
120+ $ categoryBucket ->getValues (),
121+ $ categoryIdsRequested ,
122+ $ storeId
123+ );
124+ $ categoryBucketResolved = $ this ->bucketFactory ->create (
125+ [
126+ 'name ' => self ::CATEGORY_BUCKET ,
127+ 'values ' => $ bucketValuesFiltered
128+ ]
129+ );
130+ $ buckets [self ::CATEGORY_BUCKET ] = $ categoryBucketResolved ;
102131 }
103- $ categoryIdsRequested = is_array ($ categoryIdsRequested ) ? $ categoryIdsRequested : [$ categoryIdsRequested ];
104- $ bucketValuesFiltered = $ this ->filterBucketValues (
105- $ categoryBucket ->getValues (),
106- $ categoryIdsRequested ,
107- $ storeId
108- );
109- $ categoryBucketResolved = $ this ->bucketFactory ->create (
110- [
111- 'name ' => self ::CATEGORY_BUCKET ,
112- 'values ' => $ bucketValuesFiltered
113- ]
114- );
115- $ buckets [self ::CATEGORY_BUCKET ] = $ categoryBucketResolved ;
132+
116133 return $ this ->aggregationFactory ->create ([self ::BUCKETS_NAME => $ buckets ]);
117134 }
118135
0 commit comments