7
7
8
8
namespace Magento \CatalogGraphQl \DataProvider \Product \LayeredNavigation \Builder ;
9
9
10
+ use Magento \CatalogGraphQl \DataProvider \Product \LayeredNavigation \AttributeOptionProvider ;
10
11
use Magento \CatalogGraphQl \DataProvider \Product \LayeredNavigation \LayerBuilderInterface ;
11
12
use Magento \Framework \Api \Search \AggregationInterface ;
13
+ use Magento \Framework \Api \Search \AggregationValueInterface ;
12
14
use Magento \Framework \Api \Search \BucketInterface ;
13
15
use Magento \CatalogGraphQl \DataProvider \Product \LayeredNavigation \Formatter \LayerFormatter ;
14
- use Magento \ Catalog \ Api \ ProductAttributeRepositoryInterface ;
16
+ use Zend_Db_Statement_Exception ;
15
17
16
18
/**
17
19
* @inheritdoc
@@ -29,9 +31,9 @@ class Price implements LayerBuilderInterface
29
31
private $ layerFormatter ;
30
32
31
33
/**
32
- * @var ProductAttributeRepositoryInterface
34
+ * @var AttributeOptionProvider
33
35
*/
34
- private $ attributeRepository ;
36
+ private $ attributeOptionProvider ;
35
37
36
38
/**
37
39
* @var array
@@ -45,14 +47,14 @@ class Price implements LayerBuilderInterface
45
47
46
48
/**
47
49
* @param LayerFormatter $layerFormatter
48
- * @param ProductAttributeRepositoryInterface $attributeRepository
50
+ * @param AttributeOptionProvider $attributeOptionProvider
49
51
*/
50
52
public function __construct (
51
53
LayerFormatter $ layerFormatter ,
52
- ProductAttributeRepositoryInterface $ attributeRepository
54
+ AttributeOptionProvider $ attributeOptionProvider
53
55
) {
54
56
$ this ->layerFormatter = $ layerFormatter ;
55
- $ this ->attributeRepository = $ attributeRepository ;
57
+ $ this ->attributeOptionProvider = $ attributeOptionProvider ;
56
58
}
57
59
58
60
/**
@@ -61,24 +63,16 @@ public function __construct(
61
63
*/
62
64
public function build (AggregationInterface $ aggregation , ?int $ storeId ): array
63
65
{
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 ] ?? [];
75
69
$ bucket = $ aggregation ->getBucket (self ::PRICE_BUCKET );
76
70
if ($ this ->isBucketEmpty ($ bucket )) {
77
71
return [];
78
72
}
79
73
80
74
$ result = $ this ->layerFormatter ->buildLayer (
81
- $ storeFrontLabel ,
75
+ $ attribute [ ' attribute_label ' ] ?? self :: $ bucketMap [ self :: PRICE_BUCKET ][ ' label ' ] ,
82
76
\count ($ bucket ->getValues ()),
83
77
self ::$ bucketMap [self ::PRICE_BUCKET ]['request_name ' ]
84
78
);
@@ -105,4 +99,42 @@ private function isBucketEmpty(?BucketInterface $bucket): bool
105
99
{
106
100
return null === $ bucket || !$ bucket ->getValues ();
107
101
}
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
+ }
108
140
}
0 commit comments