77
88namespace Magento \CatalogGraphQl \DataProvider \Product \LayeredNavigation \Builder ;
99
10+ use Magento \CatalogGraphQl \DataProvider \Product \LayeredNavigation \AttributeOptionProvider ;
1011use Magento \CatalogGraphQl \DataProvider \Product \LayeredNavigation \LayerBuilderInterface ;
1112use Magento \Framework \Api \Search \AggregationInterface ;
13+ use Magento \Framework \Api \Search \AggregationValueInterface ;
1214use Magento \Framework \Api \Search \BucketInterface ;
1315use Magento \CatalogGraphQl \DataProvider \Product \LayeredNavigation \Formatter \LayerFormatter ;
14- use Magento \ Catalog \ Api \ ProductAttributeRepositoryInterface ;
16+ use Zend_Db_Statement_Exception ;
1517
1618/**
1719 * @inheritdoc
@@ -29,9 +31,9 @@ class Price implements LayerBuilderInterface
2931 private $ layerFormatter ;
3032
3133 /**
32- * @var ProductAttributeRepositoryInterface
34+ * @var AttributeOptionProvider
3335 */
34- private $ attributeRepository ;
36+ private $ attributeOptionProvider ;
3537
3638 /**
3739 * @var array
@@ -45,14 +47,14 @@ class Price implements LayerBuilderInterface
4547
4648 /**
4749 * @param LayerFormatter $layerFormatter
48- * @param ProductAttributeRepositoryInterface $attributeRepository
50+ * @param AttributeOptionProvider $attributeOptionProvider
4951 */
5052 public function __construct (
5153 LayerFormatter $ layerFormatter ,
52- ProductAttributeRepositoryInterface $ attributeRepository
54+ AttributeOptionProvider $ attributeOptionProvider
5355 ) {
5456 $ this ->layerFormatter = $ layerFormatter ;
55- $ this ->attributeRepository = $ attributeRepository ;
57+ $ this ->attributeOptionProvider = $ attributeOptionProvider ;
5658 }
5759
5860 /**
@@ -61,24 +63,16 @@ public function __construct(
6163 */
6264 public function build (AggregationInterface $ aggregation , ?int $ storeId ): array
6365 {
64- $ storeFrontLabel = '' ;
65-
66- $ attribute = $ this ->attributeRepository ->get (
67- self ::$ bucketMap [self ::PRICE_BUCKET ]['request_name ' ]
68- );
69-
70- if ($ attribute ) {
71- $ storeFrontLabel = isset ($ attribute ->getStorelabels ()[$ storeId ]) ?
72- $ attribute ->getStorelabels ()[$ storeId ] : $ attribute ->getFrontendLabel ();
73- }
74-
66+ $ attributeOptions = $ this ->getAttributeOptions ($ aggregation , $ storeId );
67+ $ attributeCode = self ::$ bucketMap [self ::PRICE_BUCKET ]['request_name ' ];
68+ $ attribute = $ attributeOptions [$ attributeCode ] ?? [];
7569 $ bucket = $ aggregation ->getBucket (self ::PRICE_BUCKET );
7670 if ($ this ->isBucketEmpty ($ bucket )) {
7771 return [];
7872 }
7973
8074 $ result = $ this ->layerFormatter ->buildLayer (
81- $ storeFrontLabel ,
75+ $ attribute [ ' attribute_label ' ] ?? self :: $ bucketMap [ self :: PRICE_BUCKET ][ ' label ' ] ,
8276 \count ($ bucket ->getValues ()),
8377 self ::$ bucketMap [self ::PRICE_BUCKET ]['request_name ' ]
8478 );
@@ -105,4 +99,42 @@ private function isBucketEmpty(?BucketInterface $bucket): bool
10599 {
106100 return null === $ bucket || !$ bucket ->getValues ();
107101 }
102+
103+ /**
104+ * Get list of attributes with options
105+ *
106+ * @param AggregationInterface $aggregation
107+ * @param int|null $storeId
108+ * @return array
109+ * @throws Zend_Db_Statement_Exception
110+ */
111+ private function getAttributeOptions (AggregationInterface $ aggregation , ?int $ storeId ): array
112+ {
113+ $ attributeOptionIds = [];
114+ $ attributes = [];
115+
116+ $ bucket = $ aggregation ->getBucket (self ::PRICE_BUCKET );
117+
118+ if ($ this ->isBucketEmpty ($ bucket )) {
119+ return [];
120+ }
121+
122+ $ attributes [] = \preg_replace ('~_bucket$~ ' , '' , $ bucket ->getName ());
123+ $ attributeOptionIds [] = \array_map (
124+ function (AggregationValueInterface $ value ) {
125+ return $ value ->getValue ();
126+ },
127+ $ bucket ->getValues ()
128+ );
129+
130+ if (!$ attributeOptionIds ) {
131+ return [];
132+ }
133+
134+ return $ this ->attributeOptionProvider ->getOptions (
135+ \array_merge ([], ...$ attributeOptionIds ),
136+ $ storeId ,
137+ $ attributes
138+ );
139+ }
108140}
0 commit comments