Skip to content

[Backport 2.1] Add swatch option: Prevent loosing data and default value if data is not populated via adminhtml #12043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 27, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions app/code/Magento/Swatches/Model/Plugin/EavAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
namespace Magento\Swatches\Model\Plugin;

use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
use Magento\Swatches\Model\Swatch;
use Magento\Framework\Exception\InputException;
use Magento\Swatches\Model\Swatch;

/**
* Plugin model for Catalog Resource Attribute
Expand Down Expand Up @@ -118,9 +118,17 @@ protected function setProperOptionsArray(Attribute $attribute)
$swatchesArray = $attribute->getData('swatchtext');
}
if ($canReplace == true) {
$attribute->setData('option', $optionsArray);
$attribute->setData('default', $defaultValue);
$attribute->setData('swatch', $swatchesArray);
if (!empty($optionsArray)) {
$attribute->setData('option', $optionsArray);
}
if (!empty($defaultValue)) {
$attribute->setData('default', $defaultValue);
} else {
$attribute->setData('default', [0 => $attribute->getDefaultValue()]);
}
if (!empty($swatchesArray)) {
$attribute->setData('swatch', $swatchesArray);
}
}
}

Expand Down Expand Up @@ -316,7 +324,7 @@ protected function getAttributeOptionId($optionId)
*/
protected function isOptionForDelete(Attribute $attribute, $optionId)
{
$isOptionForDelete = $attribute->getData('option/delete/'.$optionId);
$isOptionForDelete = $attribute->getData('option/delete/' . $optionId);
return isset($isOptionForDelete) && $isOptionForDelete;
}

Expand All @@ -333,7 +341,7 @@ protected function loadSwatchIfExists($optionId, $storeId)
$collection->addFieldToFilter('option_id', $optionId);
$collection->addFieldToFilter('store_id', $storeId);
$collection->setPageSize(1);

$loadedSwatch = $collection->getFirstItem();
if ($loadedSwatch->getId()) {
$this->isSwatchExists = true;
Expand All @@ -356,7 +364,6 @@ protected function saveSwatchData($swatch, $optionId, $storeId, $type, $value)
if ($this->isSwatchExists) {
$swatch->setData('type', $type);
$swatch->setData('value', $value);

} else {
$swatch->setData('option_id', $optionId);
$swatch->setData('store_id', $storeId);
Expand Down