10
10
use Magento \ConfigurableProduct \Model \Product \Type \Configurable ;
11
11
use Magento \ConfigurableProduct \Model \ResourceModel \Product \Type \Configurable as ResourceModelConfigurable ;
12
12
use Magento \Framework \EntityManager \Operation \ExtensionInterface ;
13
+ use Magento \ConfigurableProduct \Api \Data \OptionInterface ;
14
+ use Magento \ConfigurableProduct \Model \Product \Type \Configurable \Attribute ;
13
15
14
16
/**
15
17
* Class SaveHandler
@@ -87,11 +89,13 @@ private function saveConfigurableProductAttributes(ProductInterface $product, ar
87
89
$ ids = [];
88
90
$ existingAttributeIds = [];
89
91
foreach ($ this ->optionRepository ->getList ($ product ->getSku ()) as $ option ) {
90
- $ existingAttributeIds [] = $ option ->getAttributeId ();
92
+ $ existingAttributeIds [$ option ->getAttributeId ()] = $ option ;
91
93
}
92
94
/** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $attribute */
93
95
foreach ($ attributes as $ attribute ) {
94
- if (!in_array ($ attribute ->getAttributeId (), $ existingAttributeIds )) {
96
+ if (!in_array ($ attribute ->getAttributeId (), array_keys ($ existingAttributeIds ))
97
+ || $ this ->isOptionChanged ($ existingAttributeIds [$ attribute ->getAttributeId ()], $ attribute )
98
+ ) {
95
99
$ attribute ->setId (null );
96
100
$ ids [] = $ this ->optionRepository ->save ($ product ->getSku (), $ attribute );
97
101
}
@@ -109,12 +113,31 @@ private function deleteConfigurableProductAttributes(ProductInterface $product)
109
113
{
110
114
$ newAttributeIds = [];
111
115
foreach ($ product ->getExtensionAttributes ()->getConfigurableProductOptions () as $ option ) {
112
- $ newAttributeIds [] = $ option ->getAttributeId ();
116
+ $ newAttributeIds [$ option ->getAttributeId ()] = $ option ;
113
117
}
114
118
foreach ($ this ->optionRepository ->getList ($ product ->getSku ()) as $ option ) {
115
- if (!in_array ($ option ->getAttributeId (), $ newAttributeIds )) {
119
+ if (!in_array ($ option ->getAttributeId (), array_keys ($ newAttributeIds ))
120
+ || $ this ->isOptionChanged ($ option , $ newAttributeIds [$ option ->getAttributeId ()])
121
+ ) {
116
122
$ this ->optionRepository ->deleteById ($ product ->getSku (), $ option ->getId ());
117
123
}
118
124
}
119
125
}
126
+
127
+ /**
128
+ * Check if existing option is changed
129
+ *
130
+ * @param OptionInterface $option
131
+ * @param Attribute $attribute
132
+ * @return bool
133
+ */
134
+ private function isOptionChanged (OptionInterface $ option , Attribute $ attribute )
135
+ {
136
+ if ($ option ->getLabel () == $ attribute ->getLabel ()
137
+ && $ option ->getPosition () == $ attribute ->getPosition ()
138
+ ) {
139
+ return false ;
140
+ }
141
+ return true ;
142
+ }
120
143
}
0 commit comments