Skip to content

Commit 1ea5060

Browse files
committed
[Backport 2.1] add swatch option: prevent loosing data and default value if data is not populated via adminhtml
1 parent 9d75bca commit 1ea5060

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

app/code/Magento/Swatches/Model/Plugin/EavAttribute.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
namespace Magento\Swatches\Model\Plugin;
77

88
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
9-
use Magento\Swatches\Model\Swatch;
109
use Magento\Framework\Exception\InputException;
10+
use Magento\Swatches\Model\Swatch;
1111

1212
/**
1313
* Plugin model for Catalog Resource Attribute
@@ -118,9 +118,17 @@ protected function setProperOptionsArray(Attribute $attribute)
118118
$swatchesArray = $attribute->getData('swatchtext');
119119
}
120120
if ($canReplace == true) {
121-
$attribute->setData('option', $optionsArray);
122-
$attribute->setData('default', $defaultValue);
123-
$attribute->setData('swatch', $swatchesArray);
121+
if (!empty($optionsArray)) {
122+
$attribute->setData('option', $optionsArray);
123+
}
124+
if (!empty($defaultValue)) {
125+
$attribute->setData('default', $defaultValue);
126+
} else {
127+
$attribute->setData('default', [0 => $attribute->getDefaultValue()]);
128+
}
129+
if (!empty($swatchesArray)) {
130+
$attribute->setData('swatch', $swatchesArray);
131+
}
124132
}
125133
}
126134

@@ -316,7 +324,7 @@ protected function getAttributeOptionId($optionId)
316324
*/
317325
protected function isOptionForDelete(Attribute $attribute, $optionId)
318326
{
319-
$isOptionForDelete = $attribute->getData('option/delete/'.$optionId);
327+
$isOptionForDelete = $attribute->getData('option/delete/' . $optionId);
320328
return isset($isOptionForDelete) && $isOptionForDelete;
321329
}
322330

@@ -333,7 +341,7 @@ protected function loadSwatchIfExists($optionId, $storeId)
333341
$collection->addFieldToFilter('option_id', $optionId);
334342
$collection->addFieldToFilter('store_id', $storeId);
335343
$collection->setPageSize(1);
336-
344+
337345
$loadedSwatch = $collection->getFirstItem();
338346
if ($loadedSwatch->getId()) {
339347
$this->isSwatchExists = true;
@@ -356,7 +364,6 @@ protected function saveSwatchData($swatch, $optionId, $storeId, $type, $value)
356364
if ($this->isSwatchExists) {
357365
$swatch->setData('type', $type);
358366
$swatch->setData('value', $value);
359-
360367
} else {
361368
$swatch->setData('option_id', $optionId);
362369
$swatch->setData('store_id', $storeId);

0 commit comments

Comments
 (0)