10
10
use Magento \Framework \Exception \InputException ;
11
11
use Magento \Framework \Serialize \Serializer \Json ;
12
12
use Magento \Swatches \Model \Swatch ;
13
+ use Magento \Swatches \Model \ResourceModel \Swatch as SwatchResource ;
13
14
14
15
/**
15
16
* Plugin model for Catalog Resource Attribute
@@ -18,6 +19,11 @@ class EavAttribute
18
19
{
19
20
const DEFAULT_STORE_ID = 0 ;
20
21
22
+ /**
23
+ * @var SwatchResource
24
+ */
25
+ private $ swatchResource ;
26
+
21
27
/**
22
28
* Base option title used for string operations to detect is option already exists or new
23
29
*/
@@ -64,17 +70,20 @@ class EavAttribute
64
70
* @param \Magento\Swatches\Model\SwatchFactory $swatchFactory
65
71
* @param \Magento\Swatches\Helper\Data $swatchHelper
66
72
* @param Json|null $serializer
73
+ * @param SwatchResource|null $swatchResource
67
74
*/
68
75
public function __construct (
69
76
\Magento \Swatches \Model \ResourceModel \Swatch \CollectionFactory $ collectionFactory ,
70
77
\Magento \Swatches \Model \SwatchFactory $ swatchFactory ,
71
78
\Magento \Swatches \Helper \Data $ swatchHelper ,
72
- Json $ serializer = null
79
+ Json $ serializer = null ,
80
+ SwatchResource $ swatchResource = null
73
81
) {
74
82
$ this ->swatchCollectionFactory = $ collectionFactory ;
75
83
$ this ->swatchFactory = $ swatchFactory ;
76
84
$ this ->swatchHelper = $ swatchHelper ;
77
85
$ this ->serializer = $ serializer ?: ObjectManager::getInstance ()->create (Json::class);
86
+ $ this ->swatchResource = $ swatchResource ?: ObjectManager::getInstance ()->create (SwatchResource::class);
78
87
}
79
88
80
89
/**
@@ -148,6 +157,7 @@ protected function setProperOptionsArray(Attribute $attribute)
148
157
* Prepare attribute for conversion from any swatch type to dropdown
149
158
*
150
159
* @param Attribute $attribute
160
+ * @throws \Magento\Framework\Exception\LocalizedException
151
161
* @return void
152
162
*/
153
163
protected function convertSwatchToDropdown (Attribute $ attribute )
@@ -157,6 +167,7 @@ protected function convertSwatchToDropdown(Attribute $attribute)
157
167
if (!empty ($ additionalData )) {
158
168
$ additionalData = $ this ->serializer ->unserialize ($ additionalData );
159
169
if (is_array ($ additionalData ) && isset ($ additionalData [Swatch::SWATCH_INPUT_TYPE_KEY ])) {
170
+ $ this ->cleanEavAttributeOptionSwatchValues ($ attribute ->getOption ());
160
171
unset($ additionalData [Swatch::SWATCH_INPUT_TYPE_KEY ]);
161
172
$ attribute ->setData ('additional_data ' , $ this ->serializer ->serialize ($ additionalData ));
162
173
}
@@ -235,6 +246,7 @@ protected function saveSwatchParams(Attribute $attribute)
235
246
{
236
247
if ($ this ->swatchHelper ->isVisualSwatch ($ attribute )) {
237
248
$ this ->processVisualSwatch ($ attribute );
249
+ $ this ->cleanTextSwatchValuesAfterSwitch ($ attribute ->getOptiontext ());
238
250
} elseif ($ this ->swatchHelper ->isTextSwatch ($ attribute )) {
239
251
$ this ->processTextualSwatch ($ attribute );
240
252
}
@@ -267,6 +279,33 @@ protected function processVisualSwatch(Attribute $attribute)
267
279
}
268
280
}
269
281
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
+
270
309
/**
271
310
* @param string $value
272
311
* @return int
0 commit comments