diff --git a/vendor/magento/module-catalog/Model/ProductRepository.php b/vendor/magento/module-catalog/Model/ProductRepository.php index c26a5d1bc56..27705ab4015 100644 --- a/vendor/magento/module-catalog/Model/ProductRepository.php +++ b/vendor/magento/module-catalog/Model/ProductRepository.php @@ -9,6 +9,7 @@ namespace Magento\Catalog\Model; use Magento\Catalog\Api\CategoryLinkManagementInterface; use Magento\Catalog\Api\Data\ProductExtension; use Magento\Catalog\Api\Data\ProductInterface; +use Magento\Catalog\Model\Attribute\ScopeOverriddenValue; use Magento\Catalog\Model\Product\Gallery\MimeTypeExtensionMap; use Magento\Catalog\Model\ProductRepository\MediaGalleryProcessor; use Magento\Catalog\Model\ResourceModel\Product\Collection; @@ -28,6 +29,8 @@ use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\Exception\StateException; use Magento\Framework\Exception\TemporaryState\CouldNotSaveException as TemporaryCouldNotSaveException; use Magento\Framework\Exception\ValidatorException; +use Magento\Store\Model\Store; +use Magento\Catalog\Api\Data\EavAttributeInterface; /** * Product Repository. @@ -177,6 +180,11 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa */ private $linkManagement; + /** + * @var ScopeOverriddenValue + */ + private $scopeOverriddenValue; + /** * ProductRepository constructor. * @param ProductFactory $productFactory @@ -204,6 +212,7 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa * @param int $cacheLimit [optional] * @param ReadExtensions $readExtensions * @param CategoryLinkManagementInterface $linkManagement + * @param ScopeOverriddenValue|null $scopeOverriddenValue * @SuppressWarnings(PHPMD.ExcessiveParameterList) * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ @@ -232,7 +241,8 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa \Magento\Framework\Serialize\Serializer\Json $serializer = null, $cacheLimit = 1000, ReadExtensions $readExtensions = null, - CategoryLinkManagementInterface $linkManagement = null + CategoryLinkManagementInterface $linkManagement = null, + ?ScopeOverriddenValue $scopeOverriddenValue = null ) { $this->productFactory = $productFactory; $this->collectionFactory = $collectionFactory; @@ -259,6 +269,8 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa ->get(ReadExtensions::class); $this->linkManagement = $linkManagement ?: \Magento\Framework\App\ObjectManager::getInstance() ->get(CategoryLinkManagementInterface::class); + $this->scopeOverriddenValue = $scopeOverriddenValue ?: \Magento\Framework\App\ObjectManager::getInstance() + ->get(ScopeOverriddenValue::class); } /** @@ -513,9 +525,12 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa { $assignToCategories = false; $tierPrices = $product->getData('tier_price'); + $productDataToChange = $product->getData(); try { - $existingProduct = $product->getId() ? $this->getById($product->getId()) : $this->get($product->getSku()); + $existingProduct = $product->getId() ? + $this->getById($product->getId()) : + $this->get($product->getSku()); $product->setData( $this->resourceModel->getLinkField(), @@ -545,7 +560,6 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa } $productDataArray['store_id'] = (int)$this->storeManager->getStore()->getId(); $product = $this->initializeProductData($productDataArray, empty($existingProduct)); - $this->processLinks($product, $productLinks); if (isset($productDataArray['media_gallery'])) { $this->processMediaGallery($product, $productDataArray['media_gallery']['images']); @@ -566,6 +580,48 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa $product->setData('tier_price', $tierPrices); } + try { + $stores = $product->getStoreIds(); + $websites = $product->getWebsiteIds(); + } catch (NoSuchEntityException $exception) { + $stores = null; + $websites = null; + } + + if (!empty($existingProduct) && is_array($stores) && is_array($websites)) { + $hasDataChanged = false; + $productAttributes = $product->getAttributes(); + if ($productAttributes !== null + && $product->getStoreId() !== Store::DEFAULT_STORE_ID + && (count($stores) > 1 || count($websites) === 1) + ) { + foreach ($productAttributes as $attribute) { + $attributeCode = $attribute->getAttributeCode(); + $value = $product->getData($attributeCode); + if ($existingProduct->getData($attributeCode) === $value + && $attribute->getScope() !== EavAttributeInterface::SCOPE_GLOBAL_TEXT + && !is_array($value) + && $attribute->getData('frontend_input') !== 'media_image' + && !$attribute->isStatic() + && !array_key_exists($attributeCode, $productDataToChange) + && $value !== null + && !$this->scopeOverriddenValue->containsValue( + ProductInterface::class, + $product, + $attributeCode, + $product->getStoreId() + ) + ) { + $product->setData($attributeCode); + $hasDataChanged = true; + } + } + if ($hasDataChanged) { + $product->setData('_edit_mode', true); + } + } + } + $this->saveProduct($product); if ($assignToCategories === true && $product->getCategoryIds()) { $this->linkManagement->assignProductToCategories(