Skip to content

Commit 94539b7

Browse files
committed
#12695: Unable to change attribute type from swatch to dropdown
- fixes after code review.
1 parent 39b1ef3 commit 94539b7

File tree

5 files changed

+44
-22
lines changed

5 files changed

+44
-22
lines changed

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class EavAttribute
2222
/**
2323
* @var SwatchResource
2424
*/
25-
protected $swatchResource;
25+
private $swatchResource;
2626

2727
/**
2828
* Base option title used for string operations to detect is option already exists or new
@@ -70,19 +70,20 @@ class EavAttribute
7070
* @param \Magento\Swatches\Model\SwatchFactory $swatchFactory
7171
* @param \Magento\Swatches\Helper\Data $swatchHelper
7272
* @param Json|null $serializer
73+
* @param SwatchResource|null $swatchResource
7374
*/
7475
public function __construct(
7576
\Magento\Swatches\Model\ResourceModel\Swatch\CollectionFactory $collectionFactory,
7677
\Magento\Swatches\Model\SwatchFactory $swatchFactory,
77-
SwatchResource $swatchResource,
7878
\Magento\Swatches\Helper\Data $swatchHelper,
79-
Json $serializer = null
79+
Json $serializer = null,
80+
SwatchResource $swatchResource = null
8081
) {
8182
$this->swatchCollectionFactory = $collectionFactory;
8283
$this->swatchFactory = $swatchFactory;
8384
$this->swatchHelper = $swatchHelper;
8485
$this->serializer = $serializer ?: ObjectManager::getInstance()->create(Json::class);
85-
$this->swatchResource = $swatchResource;
86+
$this->swatchResource = $swatchResource ?: ObjectManager::getInstance()->create(SwatchResource::class);
8687
}
8788

8889
/**
@@ -183,12 +184,12 @@ protected function convertSwatchToDropdown(Attribute $attribute)
183184
*/
184185
private function cleanEavAttributeOptionSwatchValues(Attribute $attribute)
185186
{
186-
$optionsIDs = [];
187187
if (count($attribute->getOption())) {
188188
$options = $attribute->getOption();
189-
foreach ($options['value'] as $id => $item) {
190-
$optionsIDs[] = $id;
191-
}
189+
// foreach ($options['value'] as $id => $item) {
190+
// $optionsIDs[] = $id;
191+
// }
192+
$optionsIDs = array_keys($options['value']);
192193

193194
$this->swatchResource->clearSwatchOptionByOptionId($optionsIDs);
194195
}
@@ -309,12 +310,8 @@ private function cleanTextSwatchValuesAfterSwitch( Attribute $attribute)
309310
if (count($attribute->getOptiontext())) {
310311
$options = $attribute->getOptiontext();
311312
if (count($options)) {
312-
foreach ($options['value'] as $id => $item) {
313-
$optionsIDs[] = $id;
314-
}
315-
316-
$type = Swatch::SWATCH_TYPE_TEXTUAL;
317-
$this->swatchResource->clearSwatchOptionTextByOptionId($optionsIDs, $type);
313+
$optionsIDs[] = array_keys($options['value']);
314+
$this->swatchResource->clearSwatchOptionTextByOptionId($optionsIDs, Swatch::SWATCH_TYPE_TEXTUAL);
318315
}
319316
}
320317
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ public function clearSwatchOptionByOptionId($optionIDs)
5454
}
5555
}
5656

57+
/**
58+
* @param $optionIDs
59+
* @param $type
60+
* @throws \Magento\Framework\Exception\LocalizedException
61+
*/
5762
public function clearSwatchOptionTextByOptionId($optionIDs, $type)
5863
{
5964
foreach ($optionIDs as $optionId) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function __construct(
6767
*
6868
* @return array
6969
*/
70-
public function getCodes()
70+
public function getCodes()
7171
{
7272
if ($this->swatchAttributeCodes === null) {
7373
$swatchAttributeCodesCache = $this->cache->load($this->cacheKey);

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
99
use Magento\Catalog\Model\Product;
1010
use Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute;
11+
use \Magento\Swatches\Helper\Data as SwatchesHelper;
12+
use Magento\Framework\App\ObjectManager;
1113

1214
/**
1315
* Provide list of swatch attributes for product.
@@ -17,7 +19,7 @@ class SwatchAttributesProvider
1719
/**
1820
* @var \Magento\Swatches\Helper\Data
1921
*/
20-
protected $swatchHelper;
22+
protected $swatchesHelper;
2123
/**
2224
* @var Configurable
2325
*/
@@ -35,17 +37,20 @@ class SwatchAttributesProvider
3537
private $attributesPerProduct;
3638

3739
/**
38-
* @param Configurable $typeConfigurable
40+
* SwatchAttributesProvider constructor.
41+
*
42+
* @param Configurable $typeConfigurable
3943
* @param SwatchAttributeCodes $swatchAttributeCodes
44+
* @param SwatchesHelper|null $swatchHelper
4045
*/
4146
public function __construct(
4247
Configurable $typeConfigurable,
4348
SwatchAttributeCodes $swatchAttributeCodes,
44-
\Magento\Swatches\Helper\Data\Proxy $swatchHelper
49+
SwatchesHelper $swatchHelper = null
4550
) {
4651
$this->typeConfigurable = $typeConfigurable;
4752
$this->swatchAttributeCodes = $swatchAttributeCodes;
48-
$this->swatchHelper = $swatchHelper;
53+
$this->swatchesHelper = $swatchHelper ?: ObjectManager::getInstance()->create(SwatchesHelper::class);
4954
}
5055

5156
/**
@@ -66,17 +71,27 @@ public function provide(Product $product)
6671

6772
$swatchAttributes = [];
6873
foreach ($configurableAttributes as $configurableAttribute) {
69-
// Due to $this->swatchAttributeCodes->getCodes() - return swatch data for every attribute.
70-
if ($this->swatchHelper->isSwatchAttribute($configurableAttribute->getProductAttribute())) {
74+
if ($this->getIsSwatchAttribute($configurableAttribute->getProductAttribute())) {
7175
if (array_key_exists($configurableAttribute->getAttributeId(), $swatchAttributeCodeMap)) {
7276
$swatchAttributes[$configurableAttribute->getAttributeId()]
7377
= $configurableAttribute->getProductAttribute();
7478
}
75-
7679
}
7780
}
7881
$this->attributesPerProduct[$product->getId()] = $swatchAttributes;
7982
}
8083
return $this->attributesPerProduct[$product->getId()];
8184
}
85+
86+
/**
87+
* This method introduced only for the case when customer already has converted attribute.
88+
*
89+
* @param \Magento\Catalog\Model\ResourceModel\Eav\Attribute $productAttribute
90+
* @return bool
91+
* @deprecated
92+
*/
93+
private function getIsSwatchAttribute($productAttribute)
94+
{
95+
return $this->swatchesHelper->isSwatchAttribute($productAttribute);
96+
}
8297
}

app/code/Magento/Swatches/etc/di.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,9 @@
8181
</argument>
8282
</arguments>
8383
</type>
84+
<type name="\Magento\Swatches\Model\SwatchAttributesProvider">
85+
<arguments>
86+
<argument name="swatchHelper" xsi:type="object">\Magento\Swatches\Helper\Data\Proxy</argument>
87+
</arguments>
88+
</type>
8489
</config>

0 commit comments

Comments
 (0)