12
12
use Magento \Elasticsearch \Model \Adapter \BatchDataMapperInterface ;
13
13
use Magento \Elasticsearch \Model \Adapter \FieldType \Date as DateFieldType ;
14
14
use Magento \AdvancedSearch \Model \Adapter \DataMapper \AdditionalFieldsProviderInterface ;
15
+ use Magento \Eav \Api \Data \AttributeOptionInterface ;
15
16
16
17
/**
17
18
* Map product index data to search engine metadata
18
19
*/
19
20
class ProductDataMapper implements BatchDataMapperInterface
20
21
{
22
+ /**
23
+ * @var AttributeOptionInterface[]
24
+ */
25
+ private $ attributeOptionsCache ;
26
+
21
27
/**
22
28
* @var Builder
23
29
*/
@@ -95,6 +101,7 @@ public function __construct(
95
101
$ this ->excludedAttributes = array_merge ($ this ->defaultExcludedAttributes , $ excludedAttributes );
96
102
$ this ->additionalFieldsProvider = $ additionalFieldsProvider ;
97
103
$ this ->dataProvider = $ dataProvider ;
104
+ $ this ->attributeOptionsCache = [];
98
105
}
99
106
100
107
/**
@@ -272,7 +279,13 @@ private function isAttributeDate(Attribute $attribute): bool
272
279
private function getValuesLabels (Attribute $ attribute , array $ attributeValues ): array
273
280
{
274
281
$ attributeLabels = [];
275
- foreach ($ attribute ->getOptions () as $ option ) {
282
+
283
+ $ options = $ this ->getAttributeOptions ($ attribute );
284
+ if (empty ($ options )) {
285
+ return $ attributeLabels ;
286
+ }
287
+
288
+ foreach ($ options as $ option ) {
276
289
if (\in_array ($ option ->getValue (), $ attributeValues )) {
277
290
$ attributeLabels [] = $ option ->getLabel ();
278
291
}
@@ -281,6 +294,22 @@ private function getValuesLabels(Attribute $attribute, array $attributeValues):
281
294
return $ attributeLabels ;
282
295
}
283
296
297
+ /**
298
+ * Retrieve options for attribute
299
+ *
300
+ * @param Attribute $attribute
301
+ * @return array
302
+ */
303
+ private function getAttributeOptions (Attribute $ attribute ): array
304
+ {
305
+ if (!isset ($ this ->attributeOptionsCache [$ attribute ->getId ()])) {
306
+ $ options = $ attribute ->getOptions () ?? [];
307
+ $ this ->attributeOptionsCache [$ attribute ->getId ()] = $ options ;
308
+ }
309
+
310
+ return $ this ->attributeOptionsCache [$ attribute ->getId ()];
311
+ }
312
+
284
313
/**
285
314
* Retrieve value for field. If field have only one value this method return it.
286
315
* Otherwise will be returned array of these values.
0 commit comments