diff --git a/app/code/Magento/Catalog/Model/ProductRepository.php b/app/code/Magento/Catalog/Model/ProductRepository.php index e814dc03cf37f..2b9ee8f862907 100644 --- a/app/code/Magento/Catalog/Model/ProductRepository.php +++ b/app/code/Magento/Catalog/Model/ProductRepository.php @@ -333,8 +333,13 @@ protected function initializeProductData(array $productData, $createNew) $product->setWebsiteIds([$this->storeManager->getStore(true)->getWebsiteId()]); } } else { - unset($this->instances[$productData['sku']]); - $product = $this->get($productData['sku']); + if (!empty($productData['id'])) { + unset($this->instancesById[$productData['id']]); + $product = $this->getById($productData['id']); + } else { + unset($this->instances[$productData['sku']]); + $product = $this->get($productData['sku']); + } } foreach ($productData as $key => $value) { @@ -562,7 +567,7 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO $tierPrices = $product->getData('tier_price'); try { - $existingProduct = $this->get($product->getSku()); + $existingProduct = $product->getId() ? $this->getById($product->getId()) : $this->get($product->getSku()); $product->setData( $this->resourceModel->getLinkField(), diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php index c98705b4eda63..2a9a867fa20b5 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php @@ -610,7 +610,7 @@ public function testSaveException() ->willReturn(true); $this->resourceModelMock->expects($this->once())->method('save')->with($this->productMock) ->willThrowException(new \Magento\Eav\Model\Entity\Attribute\Exception(__('123'))); - $this->productMock->expects($this->once())->method('getId')->willReturn(null); + $this->productMock->expects($this->exactly(2))->method('getId')->willReturn(null); $this->extensibleDataObjectConverterMock ->expects($this->once()) ->method('toNestedArray') @@ -634,7 +634,7 @@ public function testSaveInvalidProductException() $this->initializationHelperMock->expects($this->never())->method('initialize'); $this->resourceModelMock->expects($this->once())->method('validate')->with($this->productMock) ->willReturn(['error1', 'error2']); - $this->productMock->expects($this->never())->method('getId'); + $this->productMock->expects($this->once())->method('getId')->willReturn(null); $this->extensibleDataObjectConverterMock ->expects($this->once()) ->method('toNestedArray') diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Model/ProductRepositoryTest.php b/dev/tests/integration/testsuite/Magento/Catalog/Model/ProductRepositoryTest.php new file mode 100644 index 0000000000000..9518e9c0cdf4f --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Catalog/Model/ProductRepositoryTest.php @@ -0,0 +1,52 @@ +productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class); + } + + /** + * Test Product Repository can change(update) "sku" for given product. + * + * @magentoDataFixture Magento/Catalog/_files/product_simple.php + * @magentoAppArea adminhtml + */ + public function testUpdateProductSku() + { + $newSku = 'simple-edited'; + $productId = Bootstrap::getObjectManager()->get(ProductResource::class)->getIdBySku('simple'); + $initialProduct = Bootstrap::getObjectManager()->create(Product::class)->load($productId); + + $initialProduct->setSku($newSku); + $this->productRepository->save($initialProduct); + + $updatedProduct = Bootstrap::getObjectManager()->create(Product::class); + $updatedProduct->load($productId); + self::assertSame($newSku, $updatedProduct->getSku()); + } +}