Skip to content

Commit 0b079e0

Browse files
committed
#12695: Unable to change attribute type from swatch to dropdown
- fixes after code review.
1 parent 3d76096 commit 0b079e0

File tree

2 files changed

+28
-42
lines changed

2 files changed

+28
-42
lines changed

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

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ protected function convertSwatchToDropdown(Attribute $attribute)
167167
$additionalData = $this->serializer->unserialize($additionalData);
168168
if (is_array($additionalData) && isset($additionalData[Swatch::SWATCH_INPUT_TYPE_KEY])) {
169169
if ($additionalData[Swatch::SWATCH_INPUT_TYPE_KEY] == Swatch::SWATCH_INPUT_TYPE_VISUAL) {
170-
$this->cleanEavAttributeOptionSwatchValues($attribute);
170+
$this->cleanEavAttributeOptionSwatchValues($attribute->getOption());
171171
}
172172
unset($additionalData[Swatch::SWATCH_INPUT_TYPE_KEY]);
173173
$attribute->setData('additional_data', $this->serializer->serialize($additionalData));
@@ -176,22 +176,6 @@ protected function convertSwatchToDropdown(Attribute $attribute)
176176
}
177177
}
178178

179-
/**
180-
* Clean swatch option values after switching to the dropdown type.
181-
*
182-
* @param Attribute $attribute
183-
* @throws \Magento\Framework\Exception\LocalizedException
184-
*/
185-
private function cleanEavAttributeOptionSwatchValues(Attribute $attribute)
186-
{
187-
if (count($attribute->getOption())) {
188-
$options = $attribute->getOption();
189-
$optionsIDs = array_keys($options['value']);
190-
191-
$this->swatchResource->clearSwatchOptionByOptionId($optionsIDs);
192-
}
193-
}
194-
195179
/**
196180
* Creates array which link new option ids
197181
*
@@ -263,7 +247,7 @@ protected function saveSwatchParams(Attribute $attribute)
263247
{
264248
if ($this->swatchHelper->isVisualSwatch($attribute)) {
265249
$this->processVisualSwatch($attribute);
266-
$this->cleanTextSwatchValuesAfterSwitch($attribute);
250+
$this->cleanTextSwatchValuesAfterSwitch($attribute->getOptiontext());
267251
} elseif ($this->swatchHelper->isTextSwatch($attribute)) {
268252
$this->processTextualSwatch($attribute);
269253
}
@@ -296,23 +280,34 @@ protected function processVisualSwatch(Attribute $attribute)
296280
}
297281
}
298282

283+
299284
/**
300-
* Cleaning the text type of swatch option values after switching.
285+
* Clean swatch option values after switching to the dropdown type.
301286
*
302-
* @param $attribute
287+
* @param array $attributeOptions
288+
* @param null $swatchType
289+
* @throws \Magento\Framework\Exception\LocalizedException
303290
*/
304-
private function cleanTextSwatchValuesAfterSwitch( Attribute $attribute)
291+
private function cleanEavAttributeOptionSwatchValues($attributeOptions, $swatchType = null)
305292
{
306-
$optionsIDs = [];
307-
if (count($attribute->getOptiontext())) {
308-
$options = $attribute->getOptiontext();
309-
if (count($options) && isset($options['value'])) {
310-
$optionsIDs = array_keys($options['value']);
311-
$this->swatchResource->clearSwatchOptionTextByOptionId($optionsIDs, Swatch::SWATCH_TYPE_TEXTUAL);
312-
}
293+
if (count($attributeOptions) && isset($attributeOptions['value'])) {
294+
$optionsIDs = array_keys($attributeOptions['value']);
295+
296+
$this->swatchResource->clearSwatchOptionByOptionIdAndType($optionsIDs, $swatchType);
313297
}
314298
}
315299

300+
/**
301+
* Cleaning the text type of swatch option values after switching.
302+
*
303+
* @param array $attributeOptions
304+
* @throws \Magento\Framework\Exception\LocalizedException
305+
*/
306+
private function cleanTextSwatchValuesAfterSwitch($attributeOptions)
307+
{
308+
$this->cleanEavAttributeOptionSwatchValues($attributeOptions, Swatch::SWATCH_TYPE_TEXTUAL);
309+
}
310+
316311
/**
317312
* @param string $value
318313
* @return int

app/code/Magento/Swatches/Model/ResourceModel/Swatch.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,19 @@ public function saveDefaultSwatchOption($id, $defaultValue)
4242
* Cleaned swatch option values when switching to dropdown input type
4343
*
4444
* @param $optionIDs
45+
* @param $type
4546
* @throws \Magento\Framework\Exception\LocalizedException
4647
*/
47-
public function clearSwatchOptionByOptionId($optionIDs)
48+
public function clearSwatchOptionByOptionIdAndType($optionIDs, $type = null)
4849
{
4950
if (count($optionIDs)) {
5051
foreach ($optionIDs as $optionId) {
5152
$where = ['option_id' => $optionId];
53+
if ($type !== null) {
54+
$where['type = ?'] = $type;
55+
}
5256
$this->getConnection()->delete($this->getMainTable(), $where);
5357
}
5458
}
5559
}
56-
57-
/**
58-
* @param $optionIDs
59-
* @param $type
60-
* @throws \Magento\Framework\Exception\LocalizedException
61-
*/
62-
public function clearSwatchOptionTextByOptionId($optionIDs, $type)
63-
{
64-
foreach ($optionIDs as $optionId) {
65-
$where = ['option_id = ?' => $optionId, 'type = ?' => $type];
66-
$this->getConnection()->delete($this->getMainTable(), $where);
67-
}
68-
}
6960
}

0 commit comments

Comments
 (0)