Skip to content

magento/magento2#12695: Unable to change attribute type from swatch #12771

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 16 commits into from
Jun 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
39b1ef3
magento/magento2#12695: Unable to change attribute type from swatch t…
eugene-shab Dec 15, 2017
94539b7
magento/magento2#12695: Unable to change attribute type from swatch t…
eugene-shab Dec 21, 2017
3d76096
magento/magento2#12695: Unable to change attribute type from swatch t…
eugene-shab Dec 21, 2017
0b079e0
magento/magento2#12695: Unable to change attribute type from swatch t…
eugene-shab Dec 21, 2017
71d30b7
magento/magento2#12695: Unable to change attribute type from swatch t…
eugene-shab Dec 23, 2017
47b2f48
magento/magento2#12695: Unable to change attribute type from swatch t…
eugene-shab Dec 23, 2017
707f1cd
magento/magento2#12695: Unable to change attribute type from swatch t…
eugene-shab Dec 26, 2017
9153fe2
Added after merge.
eugene-shab Mar 19, 2018
1bd4acf
Merge branch '2.2-develop' into 12695
eugene-shab Mar 19, 2018
f499027
Merge pull request #1 from magento/2.2-develop
eugene-shab Mar 19, 2018
3fa9ed2
Merge branch '2.2-develop' of github.com:eugene-shab/magento2 into 12695
eugene-shab May 3, 2018
8fa792a
magento/magento2#12695: Unable to change attribute type from swatch t…
eugene-shab May 3, 2018
7cd7ade
magento/magento2#12695: Unable to change attribute type from swatch t…
eugene-shab May 3, 2018
54ad76c
magento/magento2#12695: Unable to change attribute type from swatch t…
eugene-shab May 3, 2018
bd12651
magento/magento2#12695: Unable to change attribute type from swatch t…
eugene-shab May 4, 2018
cb6975c
Removed unused dependency
May 4, 2018
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
41 changes: 40 additions & 1 deletion app/code/Magento/Swatches/Model/Plugin/EavAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Magento\Framework\Exception\InputException;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Swatches\Model\Swatch;
use Magento\Swatches\Model\ResourceModel\Swatch as SwatchResource;

/**
* Plugin model for Catalog Resource Attribute
Expand All @@ -18,6 +19,11 @@ class EavAttribute
{
const DEFAULT_STORE_ID = 0;

/**
* @var SwatchResource
*/
private $swatchResource;

/**
* Base option title used for string operations to detect is option already exists or new
*/
Expand Down Expand Up @@ -64,17 +70,20 @@ class EavAttribute
* @param \Magento\Swatches\Model\SwatchFactory $swatchFactory
* @param \Magento\Swatches\Helper\Data $swatchHelper
* @param Json|null $serializer
* @param SwatchResource|null $swatchResource
*/
public function __construct(
\Magento\Swatches\Model\ResourceModel\Swatch\CollectionFactory $collectionFactory,
\Magento\Swatches\Model\SwatchFactory $swatchFactory,
\Magento\Swatches\Helper\Data $swatchHelper,
Json $serializer = null
Json $serializer = null,
SwatchResource $swatchResource = null
) {
$this->swatchCollectionFactory = $collectionFactory;
$this->swatchFactory = $swatchFactory;
$this->swatchHelper = $swatchHelper;
$this->serializer = $serializer ?: ObjectManager::getInstance()->create(Json::class);
$this->swatchResource = $swatchResource ?: ObjectManager::getInstance()->create(SwatchResource::class);
}

/**
Expand Down Expand Up @@ -148,6 +157,7 @@ protected function setProperOptionsArray(Attribute $attribute)
* Prepare attribute for conversion from any swatch type to dropdown
*
* @param Attribute $attribute
* @throws \Magento\Framework\Exception\LocalizedException
* @return void
*/
protected function convertSwatchToDropdown(Attribute $attribute)
Expand All @@ -157,6 +167,7 @@ protected function convertSwatchToDropdown(Attribute $attribute)
if (!empty($additionalData)) {
$additionalData = $this->serializer->unserialize($additionalData);
if (is_array($additionalData) && isset($additionalData[Swatch::SWATCH_INPUT_TYPE_KEY])) {
$this->cleanEavAttributeOptionSwatchValues($attribute->getOption());
unset($additionalData[Swatch::SWATCH_INPUT_TYPE_KEY]);
$attribute->setData('additional_data', $this->serializer->serialize($additionalData));
}
Expand Down Expand Up @@ -235,6 +246,7 @@ protected function saveSwatchParams(Attribute $attribute)
{
if ($this->swatchHelper->isVisualSwatch($attribute)) {
$this->processVisualSwatch($attribute);
$this->cleanTextSwatchValuesAfterSwitch($attribute->getOptiontext());
} elseif ($this->swatchHelper->isTextSwatch($attribute)) {
$this->processTextualSwatch($attribute);
}
Expand Down Expand Up @@ -267,6 +279,33 @@ protected function processVisualSwatch(Attribute $attribute)
}
}

/**
* Clean swatch option values after switching to the dropdown type.
*
* @param array $attributeOptions
* @param null $swatchType
* @throws \Magento\Framework\Exception\LocalizedException
*/
private function cleanEavAttributeOptionSwatchValues($attributeOptions, $swatchType = null)
{
if (count($attributeOptions) && isset($attributeOptions['value'])) {
$optionsIDs = array_keys($attributeOptions['value']);

$this->swatchResource->clearSwatchOptionByOptionIdAndType($optionsIDs, $swatchType);
}
}

/**
* Cleaning the text type of swatch option values after switching.
*
* @param array $attributeOptions
* @throws \Magento\Framework\Exception\LocalizedException
*/
private function cleanTextSwatchValuesAfterSwitch($attributeOptions)
{
$this->cleanEavAttributeOptionSwatchValues($attributeOptions, Swatch::SWATCH_TYPE_TEXTUAL);
}

/**
* @param string $value
* @return int
Expand Down
20 changes: 20 additions & 0 deletions app/code/Magento/Swatches/Model/ResourceModel/Swatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,24 @@ public function saveDefaultSwatchOption($id, $defaultValue)
$this->getConnection()->update($this->getTable('eav_attribute'), $bind, $where);
}
}

/**
* Cleaned swatch option values when switching to dropdown input type
*
* @param $optionIDs
* @param $type
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function clearSwatchOptionByOptionIdAndType($optionIDs, $type = null)
{
if (count($optionIDs)) {
foreach ($optionIDs as $optionId) {
$where = ['option_id' => $optionId];
if ($type !== null) {
$where['type = ?'] = $type;
}
$this->getConnection()->delete($this->getMainTable(), $where);
}
}
}
}
2 changes: 2 additions & 0 deletions app/code/Magento/Swatches/Model/SwatchAttributesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class SwatchAttributesProvider
private $swatchTypeChecker;

/**
* SwatchAttributesProvider constructor.
*
* @param Configurable $typeConfigurable
* @param SwatchAttributeCodes $swatchAttributeCodes
* @param SwatchAttributeType|null $swatchTypeChecker
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected function setUp()
{
$this->typeConfigurable = $this->createPartialMock(
Configurable::class,
['getConfigurableAttributes', 'getCodes']
['getConfigurableAttributes', 'getCodes', 'getProductAttribute']
);

$this->swatchAttributeCodes = $this->createMock(SwatchAttributeCodes::class);
Expand All @@ -65,8 +65,9 @@ public function testProvide()
$this->productMock->method('getTypeId')
->willReturn(Configurable::TYPE_CODE);

$productAttributeMock = $this->getMockBuilder(Attribute::class)
$attributeMock = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class)
->disableOriginalConstructor()
->setMethods(['setStoreId', 'getData', 'setData', 'getSource', 'hasData'])
->getMock();

$configAttributeMock = $this->createPartialMock(
Expand All @@ -79,7 +80,7 @@ public function testProvide()

$configAttributeMock
->method('getProductAttribute')
->willReturn($productAttributeMock);
->willReturn($attributeMock);

$this->typeConfigurable
->method('getConfigurableAttributes')
Expand All @@ -92,8 +93,9 @@ public function testProvide()
->willReturn($swatchAttributes);

$this->swatchTypeChecker->expects($this->once())->method('isSwatchAttribute')->willReturn(true);

$result = $this->swatchAttributeProvider->provide($this->productMock);

$this->assertEquals([1 => $productAttributeMock], $result);
$this->assertEquals([1 => $attributeMock], $result);
}
}