Skip to content

Commit dedf199

Browse files
ENGCOM-1443: #12695: Unable to change attribute type from swatch #12771
- Merge Pull Request #12771 from eugene-shab/magento2:12695 - Merged commits: 1. 39b1ef3 2. 94539b7 3. 3d76096 4. 0b079e0 5. 71d30b7 6. 47b2f48 7. 707f1cd 8. 9153fe2 9. 1bd4acf 10. f499027 11. 3fa9ed2 12. 8fa792a 13. 7cd7ade 14. 54ad76c 15. bd12651 16. cb6975c
2 parents 4bffc3a + cb6975c commit dedf199

File tree

4 files changed

+68
-5
lines changed

4 files changed

+68
-5
lines changed

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Framework\Exception\InputException;
1111
use Magento\Framework\Serialize\Serializer\Json;
1212
use Magento\Swatches\Model\Swatch;
13+
use Magento\Swatches\Model\ResourceModel\Swatch as SwatchResource;
1314

1415
/**
1516
* Plugin model for Catalog Resource Attribute
@@ -18,6 +19,11 @@ class EavAttribute
1819
{
1920
const DEFAULT_STORE_ID = 0;
2021

22+
/**
23+
* @var SwatchResource
24+
*/
25+
private $swatchResource;
26+
2127
/**
2228
* Base option title used for string operations to detect is option already exists or new
2329
*/
@@ -64,17 +70,20 @@ class EavAttribute
6470
* @param \Magento\Swatches\Model\SwatchFactory $swatchFactory
6571
* @param \Magento\Swatches\Helper\Data $swatchHelper
6672
* @param Json|null $serializer
73+
* @param SwatchResource|null $swatchResource
6774
*/
6875
public function __construct(
6976
\Magento\Swatches\Model\ResourceModel\Swatch\CollectionFactory $collectionFactory,
7077
\Magento\Swatches\Model\SwatchFactory $swatchFactory,
7178
\Magento\Swatches\Helper\Data $swatchHelper,
72-
Json $serializer = null
79+
Json $serializer = null,
80+
SwatchResource $swatchResource = null
7381
) {
7482
$this->swatchCollectionFactory = $collectionFactory;
7583
$this->swatchFactory = $swatchFactory;
7684
$this->swatchHelper = $swatchHelper;
7785
$this->serializer = $serializer ?: ObjectManager::getInstance()->create(Json::class);
86+
$this->swatchResource = $swatchResource ?: ObjectManager::getInstance()->create(SwatchResource::class);
7887
}
7988

8089
/**
@@ -148,6 +157,7 @@ protected function setProperOptionsArray(Attribute $attribute)
148157
* Prepare attribute for conversion from any swatch type to dropdown
149158
*
150159
* @param Attribute $attribute
160+
* @throws \Magento\Framework\Exception\LocalizedException
151161
* @return void
152162
*/
153163
protected function convertSwatchToDropdown(Attribute $attribute)
@@ -157,6 +167,7 @@ protected function convertSwatchToDropdown(Attribute $attribute)
157167
if (!empty($additionalData)) {
158168
$additionalData = $this->serializer->unserialize($additionalData);
159169
if (is_array($additionalData) && isset($additionalData[Swatch::SWATCH_INPUT_TYPE_KEY])) {
170+
$this->cleanEavAttributeOptionSwatchValues($attribute->getOption());
160171
unset($additionalData[Swatch::SWATCH_INPUT_TYPE_KEY]);
161172
$attribute->setData('additional_data', $this->serializer->serialize($additionalData));
162173
}
@@ -235,6 +246,7 @@ protected function saveSwatchParams(Attribute $attribute)
235246
{
236247
if ($this->swatchHelper->isVisualSwatch($attribute)) {
237248
$this->processVisualSwatch($attribute);
249+
$this->cleanTextSwatchValuesAfterSwitch($attribute->getOptiontext());
238250
} elseif ($this->swatchHelper->isTextSwatch($attribute)) {
239251
$this->processTextualSwatch($attribute);
240252
}
@@ -267,6 +279,33 @@ protected function processVisualSwatch(Attribute $attribute)
267279
}
268280
}
269281

282+
/**
283+
* Clean swatch option values after switching to the dropdown type.
284+
*
285+
* @param array $attributeOptions
286+
* @param null $swatchType
287+
* @throws \Magento\Framework\Exception\LocalizedException
288+
*/
289+
private function cleanEavAttributeOptionSwatchValues($attributeOptions, $swatchType = null)
290+
{
291+
if (count($attributeOptions) && isset($attributeOptions['value'])) {
292+
$optionsIDs = array_keys($attributeOptions['value']);
293+
294+
$this->swatchResource->clearSwatchOptionByOptionIdAndType($optionsIDs, $swatchType);
295+
}
296+
}
297+
298+
/**
299+
* Cleaning the text type of swatch option values after switching.
300+
*
301+
* @param array $attributeOptions
302+
* @throws \Magento\Framework\Exception\LocalizedException
303+
*/
304+
private function cleanTextSwatchValuesAfterSwitch($attributeOptions)
305+
{
306+
$this->cleanEavAttributeOptionSwatchValues($attributeOptions, Swatch::SWATCH_TYPE_TEXTUAL);
307+
}
308+
270309
/**
271310
* @param string $value
272311
* @return int

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,24 @@ public function saveDefaultSwatchOption($id, $defaultValue)
3737
$this->getConnection()->update($this->getTable('eav_attribute'), $bind, $where);
3838
}
3939
}
40+
41+
/**
42+
* Cleaned swatch option values when switching to dropdown input type
43+
*
44+
* @param $optionIDs
45+
* @param $type
46+
* @throws \Magento\Framework\Exception\LocalizedException
47+
*/
48+
public function clearSwatchOptionByOptionIdAndType($optionIDs, $type = null)
49+
{
50+
if (count($optionIDs)) {
51+
foreach ($optionIDs as $optionId) {
52+
$where = ['option_id' => $optionId];
53+
if ($type !== null) {
54+
$where['type = ?'] = $type;
55+
}
56+
$this->getConnection()->delete($this->getMainTable(), $where);
57+
}
58+
}
59+
}
4060
}

app/code/Magento/Swatches/Model/SwatchAttributesProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class SwatchAttributesProvider
3939
private $swatchTypeChecker;
4040

4141
/**
42+
* SwatchAttributesProvider constructor.
43+
*
4244
* @param Configurable $typeConfigurable
4345
* @param SwatchAttributeCodes $swatchAttributeCodes
4446
* @param SwatchAttributeType|null $swatchTypeChecker

app/code/Magento/Swatches/Test/Unit/Model/SwatchAttributesProviderTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected function setUp()
4444
{
4545
$this->typeConfigurable = $this->createPartialMock(
4646
Configurable::class,
47-
['getConfigurableAttributes', 'getCodes']
47+
['getConfigurableAttributes', 'getCodes', 'getProductAttribute']
4848
);
4949

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

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

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

8081
$configAttributeMock
8182
->method('getProductAttribute')
82-
->willReturn($productAttributeMock);
83+
->willReturn($attributeMock);
8384

8485
$this->typeConfigurable
8586
->method('getConfigurableAttributes')
@@ -92,8 +93,9 @@ public function testProvide()
9293
->willReturn($swatchAttributes);
9394

9495
$this->swatchTypeChecker->expects($this->once())->method('isSwatchAttribute')->willReturn(true);
96+
9597
$result = $this->swatchAttributeProvider->provide($this->productMock);
9698

97-
$this->assertEquals([1 => $productAttributeMock], $result);
99+
$this->assertEquals([1 => $attributeMock], $result);
98100
}
99101
}

0 commit comments

Comments
 (0)