diff --git a/app/code/Magento/Catalog/Model/Product/Copier.php b/app/code/Magento/Catalog/Model/Product/Copier.php index 44ebdf0f1f283..6afcb3bc8b536 100644 --- a/app/code/Magento/Catalog/Model/Product/Copier.php +++ b/app/code/Magento/Catalog/Model/Product/Copier.php @@ -3,10 +3,15 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +declare(strict_types=1); + namespace Magento\Catalog\Model\Product; use Magento\Catalog\Api\Data\ProductInterface; +use Magento\Catalog\Api\ProductRepositoryInterface; use Magento\Catalog\Model\Product; +use Magento\Framework\App\ObjectManager; +use Magento\Store\Model\Store; /** * Catalog product copier. @@ -37,16 +42,25 @@ class Copier */ protected $metadataPool; + /** + * @var ProductRepositoryInterface + */ + private $productRepository; + /** * @param CopyConstructorInterface $copyConstructor * @param \Magento\Catalog\Model\ProductFactory $productFactory + * @param ProductRepositoryInterface|null $productRepository */ public function __construct( CopyConstructorInterface $copyConstructor, - \Magento\Catalog\Model\ProductFactory $productFactory + \Magento\Catalog\Model\ProductFactory $productFactory, + ProductRepositoryInterface $productRepository = null ) { $this->productFactory = $productFactory; $this->copyConstructor = $copyConstructor; + $this->productRepository = $productRepository ?: + ObjectManager::getInstance()->get(ProductRepositoryInterface::class); } /** @@ -57,6 +71,11 @@ public function __construct( */ public function copy(Product $product) { + if ($product->getStoreId() !== Store::DEFAULT_STORE_ID) { + $productSku = $product->getSku(); + $product = $this->productRepository->get($productSku, false, Store::DEFAULT_STORE_ID); + } + $product->getWebsiteIds(); $product->getCategoryIds(); diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Product/CopierTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Product/CopierTest.php index 80b6db2a516bd..140aaa54f5a48 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Product/CopierTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Product/CopierTest.php @@ -72,10 +72,13 @@ protected function setUp() $this->productFactoryMock ); - $this->setProperties($this->_model, [ - 'optionRepository' => $this->optionRepositoryMock, - 'metadataPool' => $metadataPool, - ]); + $this->setProperties( + $this->_model, + [ + 'optionRepository' => $this->optionRepositoryMock, + 'metadataPool' => $metadataPool, + ] + ); } /** @@ -101,12 +104,16 @@ public function testCopy() 'product data' => ['product data'], ProductInterface::EXTENSION_ATTRIBUTES_KEY => $extensionAttributes, ]; + + $this->productMock->expects($this->once())->method('getStoreId')->willReturn(0); $this->productMock->expects($this->atLeastOnce())->method('getWebsiteIds'); $this->productMock->expects($this->atLeastOnce())->method('getCategoryIds'); - $this->productMock->expects($this->any())->method('getData')->willReturnMap([ - ['', null, $productData], - ['linkField', null, '1'], - ]); + $this->productMock->expects($this->any())->method('getData')->willReturnMap( + [ + ['', null, $productData], + ['linkField', null, '1'], + ] + ); $entityMock = $this->getMockForAbstractClass( \Magento\Eav\Model\Entity\AbstractEntity::class, @@ -191,9 +198,11 @@ public function testCopy() $this->metadata->expects($this->any())->method('getLinkField')->willReturn('linkField'); - $duplicateMock->expects($this->any())->method('getData')->willReturnMap([ - ['linkField', null, '2'], - ]); + $duplicateMock->expects($this->any())->method('getData')->willReturnMap( + [ + ['linkField', null, '2'], + ] + ); $this->optionRepositoryMock->expects($this->once()) ->method('duplicate') ->with($this->productMock, $duplicateMock);