14
14
15
15
/**
16
16
* Plugin model for Catalog Resource Attribute
17
+ *
18
+ * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
17
19
*/
18
20
class EavAttribute
19
21
{
@@ -29,6 +31,11 @@ class EavAttribute
29
31
*/
30
32
const BASE_OPTION_TITLE = 'option ' ;
31
33
34
+ /**
35
+ * Prefix added to option value added through API
36
+ */
37
+ private const API_OPTION_PREFIX = 'id_ ' ;
38
+
32
39
/**
33
40
* @var \Magento\Swatches\Model\ResourceModel\Swatch\CollectionFactory
34
41
*/
@@ -189,7 +196,9 @@ protected function processSwatchOptions(Attribute $attribute)
189
196
190
197
if (!empty ($ optionsArray ) && is_array ($ optionsArray )) {
191
198
$ optionsArray = $ this ->prepareOptionIds ($ optionsArray );
192
- $ attributeSavedOptions = $ attribute ->getSource ()->getAllOptions ();
199
+ $ adminStoreAttribute = clone $ attribute ;
200
+ $ adminStoreAttribute ->setStoreId (self ::DEFAULT_STORE_ID );
201
+ $ attributeSavedOptions = $ adminStoreAttribute ->getSource ()->getAllOptions ();
193
202
$ this ->prepareOptionLinks ($ optionsArray , $ attributeSavedOptions );
194
203
}
195
204
@@ -227,10 +236,9 @@ protected function prepareOptionLinks(array $optionsArray, array $attributeSaved
227
236
{
228
237
$ dependencyArray = [];
229
238
if (is_array ($ optionsArray ['value ' ])) {
230
- $ optionCounter = 1 ;
231
- foreach (array_keys ($ optionsArray ['value ' ]) as $ baseOptionId ) {
232
- $ dependencyArray [$ baseOptionId ] = $ attributeSavedOptions [$ optionCounter ]['value ' ];
233
- $ optionCounter ++;
239
+ $ options = array_column ($ attributeSavedOptions , 'value ' , 'label ' );
240
+ foreach ($ optionsArray ['value ' ] as $ id => $ labels ) {
241
+ $ dependencyArray [$ id ] = $ options [$ labels [self ::DEFAULT_STORE_ID ]];
234
242
}
235
243
}
236
244
@@ -285,7 +293,7 @@ protected function processVisualSwatch(Attribute $attribute)
285
293
* Clean swatch option values after switching to the dropdown type.
286
294
*
287
295
* @param array $attributeOptions
288
- * @param int|null $swatchType
296
+ * @param int|null $swatchType
289
297
* @throws \Magento\Framework\Exception\LocalizedException
290
298
*/
291
299
private function cleanEavAttributeOptionSwatchValues (array $ attributeOptions , int $ swatchType = null )
@@ -309,6 +317,8 @@ private function cleanTextSwatchValuesAfterSwitch(array $attributeOptions)
309
317
}
310
318
311
319
/**
320
+ * Get the visual swatch type based on its value
321
+ *
312
322
* @param string $value
313
323
* @return int
314
324
*/
@@ -368,7 +378,7 @@ protected function processTextualSwatch(Attribute $attribute)
368
378
*/
369
379
protected function getAttributeOptionId ($ optionId )
370
380
{
371
- if (substr ($ optionId , 0 , 6 ) == self ::BASE_OPTION_TITLE ) {
381
+ if (substr ($ optionId , 0 , 6 ) == self ::BASE_OPTION_TITLE || substr ( $ optionId , 0 , 3 ) == self :: API_OPTION_PREFIX ) {
372
382
$ optionId = isset ($ this ->dependencyArray [$ optionId ]) ? $ this ->dependencyArray [$ optionId ] : null ;
373
383
}
374
384
return $ optionId ;
@@ -447,13 +457,10 @@ protected function saveDefaultSwatchOptionValue(Attribute $attribute)
447
457
if (!empty ($ defaultValue )) {
448
458
/** @var \Magento\Swatches\Model\Swatch $swatch */
449
459
$ swatch = $ this ->swatchFactory ->create ();
450
- // created and removed on frontend option not exists in dependency array
451
- if (substr ($ defaultValue , 0 , 6 ) == self ::BASE_OPTION_TITLE &&
452
- isset ($ this ->dependencyArray [$ defaultValue ])
453
- ) {
454
- $ defaultValue = $ this ->dependencyArray [$ defaultValue ];
455
- }
456
- $ swatch ->getResource ()->saveDefaultSwatchOption ($ attribute ->getId (), $ defaultValue );
460
+ $ swatch ->getResource ()->saveDefaultSwatchOption (
461
+ $ attribute ->getId (),
462
+ $ this ->getAttributeOptionId ($ defaultValue )
463
+ );
457
464
}
458
465
}
459
466
@@ -503,6 +510,10 @@ protected function isOptionsValid(array $options, Attribute $attribute)
503
510
}
504
511
505
512
/**
513
+ * Modifies Attribute::usesSource() response
514
+ *
515
+ * Returns true if attribute type is swatch
516
+ *
506
517
* @param Attribute $attribute
507
518
* @param bool $result
508
519
* @return bool
0 commit comments