Skip to content

Commit 1bcb114

Browse files
committed
AC-2904-v1:: Saving product with non-default store scope causes untouched attributes to become store scoped if loaded using ProductRepository
1 parent 3b06fda commit 1bcb114

File tree

1 file changed

+8
-36
lines changed

1 file changed

+8
-36
lines changed

app/code/Magento/Catalog/Model/ProductRepository.php

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
namespace Magento\Catalog\Model;
88

99
use Magento\Catalog\Api\CategoryLinkManagementInterface;
10-
use Magento\Catalog\Api\Data\ProductAttributeInterface;
1110
use Magento\Catalog\Api\Data\ProductExtension;
1211
use Magento\Catalog\Api\Data\ProductInterface;
1312
use Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper\AttributeFilter;
@@ -188,10 +187,6 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
188187
*/
189188
private $scopeOverriddenValue;
190189

191-
/**
192-
* @var AttributeFilter
193-
*/
194-
private $attributeFilter;
195190

196191
/**
197192
* ProductRepository constructor.
@@ -214,7 +209,6 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
214209
* @param MimeTypeExtensionMap $mimeTypeExtensionMap
215210
* @param ImageProcessorInterface $imageProcessor
216211
* @param \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor
217-
* @param AttributeFilter|null $attributeFilter
218212
* @param CollectionProcessorInterface $collectionProcessor [optional]
219213
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
220214
* @param int $cacheLimit [optional]
@@ -244,7 +238,6 @@ public function __construct(
244238
MimeTypeExtensionMap $mimeTypeExtensionMap,
245239
ImageProcessorInterface $imageProcessor,
246240
\Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor,
247-
AttributeFilter $attributeFilter = null,
248241
CollectionProcessorInterface $collectionProcessor = null,
249242
\Magento\Framework\Serialize\Serializer\Json $serializer = null,
250243
$cacheLimit = 1000,
@@ -599,56 +592,35 @@ public function save(ProductInterface $product, $saveOptions = false)
599592
$websites = null;
600593
}
601594

602-
$useDefault = [];
603595
if (!empty($existingProduct) && is_array($stores) && is_array($websites)) {
604596
$hasDataChanged = false;
605597
$productAttributes = $product->getAttributes();
606598
if ($productAttributes !== null
607599
&& $product->getStoreId() !== Store::DEFAULT_STORE_ID
608600
&& (count($stores) > 1 || count($websites) >= 1)
609601
) {
610-
$imageRoles = ['image', 'small_image', 'thumbnail'];
611-
foreach ($product->getAttributes() as $attribute) {
612-
$defaultValue = $attribute->getDefaultValue();
602+
foreach ($productAttributes as $attribute) {
613603
$attributeCode = $attribute->getAttributeCode();
614604
$value = $product->getData($attributeCode);
615-
if ($defaultValue
616-
&& $defaultValue === $value
617-
&& $value !== null
605+
if ($existingProduct->getData($attributeCode) === $value
618606
&& $attribute->getScope() !== EavAttributeInterface::SCOPE_GLOBAL_TEXT
607+
&& !is_array($value)
608+
&& !$attribute->isStatic()
609+
&& $value !== null
619610
&& !$this->scopeOverriddenValue->containsValue(
620611
ProductInterface::class,
621612
$product,
622613
$attributeCode,
623614
$product->getStoreId()
624615
)
625616
) {
626-
$useDefault[$attributeCode] = '1';
627-
} elseif (!$defaultValue && $value !== null
628-
&& !in_array($attributeCode, $imageRoles)
629-
&& $attribute->getScope() !== EavAttributeInterface::SCOPE_GLOBAL_TEXT
630-
&& $existingProduct->getData($attributeCode) === $value
631-
&& $existingProduct->getOrigData($attributeCode) === $value
632-
&& !$this->scopeOverriddenValue->containsValue(
633-
ProductInterface::class,
634-
$product,
617+
$product->setData(
635618
$attributeCode,
636-
$product->getStoreId()
637-
)
638-
) {
639-
$useDefault[$attributeCode] = '1';
640-
} else {
641-
$useDefault[$attributeCode] = '0';
619+
$attributeCode === ProductAttributeInterface::CODE_SEO_FIELD_URL_KEY ? false : null
620+
);
642621
$hasDataChanged = true;
643622
}
644623
}
645-
$productDataArray = $this->attributeFilter->prepareProductAttributes(
646-
$product,
647-
$productDataArray,
648-
$useDefault
649-
);
650-
$product->setData($productDataArray);
651-
652624
if ($hasDataChanged) {
653625
$product->setData('_edit_mode', true);
654626
}

0 commit comments

Comments
 (0)