Skip to content

Commit 4e031d7

Browse files
committed
MAGETWO-86918: Saving configurable product erases and reinserts records in table "catalog_product_super_link" even with no changes
1 parent 9147391 commit 4e031d7

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

app/code/Magento/ConfigurableProduct/Model/Product/SaveHandler.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
1111
use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable as ResourceModelConfigurable;
1212
use Magento\Framework\EntityManager\Operation\ExtensionInterface;
13+
use Magento\ConfigurableProduct\Api\Data\OptionInterface;
14+
use Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute;
1315

1416
/**
1517
* Class SaveHandler
@@ -87,11 +89,13 @@ private function saveConfigurableProductAttributes(ProductInterface $product, ar
8789
$ids = [];
8890
$existingAttributeIds = [];
8991
foreach ($this->optionRepository->getList($product->getSku()) as $option) {
90-
$existingAttributeIds[] = $option->getAttributeId();
92+
$existingAttributeIds[$option->getAttributeId()] = $option;
9193
}
9294
/** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $attribute */
9395
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+
) {
9599
$attribute->setId(null);
96100
$ids[] = $this->optionRepository->save($product->getSku(), $attribute);
97101
}
@@ -109,12 +113,31 @@ private function deleteConfigurableProductAttributes(ProductInterface $product)
109113
{
110114
$newAttributeIds = [];
111115
foreach ($product->getExtensionAttributes()->getConfigurableProductOptions() as $option) {
112-
$newAttributeIds[] = $option->getAttributeId();
116+
$newAttributeIds[$option->getAttributeId()] = $option;
113117
}
114118
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+
) {
116122
$this->optionRepository->deleteById($product->getSku(), $option->getId());
117123
}
118124
}
119125
}
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+
}
120143
}

0 commit comments

Comments
 (0)