diff --git a/app/code/Magento/Bundle/Controller/Adminhtml/Bundle/Product/Edit/Duplicate.php b/app/code/Magento/Bundle/Controller/Adminhtml/Bundle/Product/Edit/Duplicate.php index 9c7040f8787bf..b24d2e852b3cf 100644 --- a/app/code/Magento/Bundle/Controller/Adminhtml/Bundle/Product/Edit/Duplicate.php +++ b/app/code/Magento/Bundle/Controller/Adminhtml/Bundle/Product/Edit/Duplicate.php @@ -6,6 +6,9 @@ */ namespace Magento\Bundle\Controller\Adminhtml\Bundle\Product\Edit; +/** + * @deprecated + */ class Duplicate extends \Magento\Catalog\Controller\Adminhtml\Product\Duplicate { } diff --git a/app/code/Magento/Catalog/Block/Adminhtml/Product/Edit.php b/app/code/Magento/Catalog/Block/Adminhtml/Product/Edit.php index 285caa974fd17..d267b58e30687 100644 --- a/app/code/Magento/Catalog/Block/Adminhtml/Product/Edit.php +++ b/app/code/Magento/Catalog/Block/Adminhtml/Product/Edit.php @@ -242,6 +242,7 @@ public function getProductSetId() /** * @return string + * @deprecated */ public function getDuplicateUrl() { diff --git a/app/code/Magento/Catalog/Controller/Adminhtml/Product/Duplicate.php b/app/code/Magento/Catalog/Controller/Adminhtml/Product/Duplicate.php index 63e52eead064c..0d22476b4404c 100644 --- a/app/code/Magento/Catalog/Controller/Adminhtml/Product/Duplicate.php +++ b/app/code/Magento/Catalog/Controller/Adminhtml/Product/Duplicate.php @@ -9,6 +9,9 @@ use Magento\Backend\App\Action; use Magento\Catalog\Controller\Adminhtml\Product; +/** + * @deprecated + */ class Duplicate extends \Magento\Catalog\Controller\Adminhtml\Product { /** diff --git a/app/code/Magento/Catalog/Controller/Adminhtml/Product/Save.php b/app/code/Magento/Catalog/Controller/Adminhtml/Product/Save.php index 825d0ee032d6c..dc8b901936551 100644 --- a/app/code/Magento/Catalog/Controller/Adminhtml/Product/Save.php +++ b/app/code/Magento/Catalog/Controller/Adminhtml/Product/Save.php @@ -157,9 +157,19 @@ public function execute() ); if ($redirectBack === 'duplicate') { - $product->unsetData('quantity_and_stock_status'); - $newProduct = $this->productCopier->copy($product); - $this->checkUniqueAttributes($product); + if ($product->getStoreId() === \Magento\Store\Model\Store::DEFAULT_STORE_ID) { + $productToDuplicate = $product; + } else { + $productToDuplicate = $this->productRepository->get( + $product->getSku(), + false, + \Magento\Store\Model\Store::DEFAULT_STORE_ID, + true + ); + } + + $productToDuplicate->unsetData('quantity_and_stock_status'); + $newProduct = $this->productCopier->copy($productToDuplicate); $this->messageManager->addSuccessMessage(__('You duplicated the product.')); } } catch (\Magento\Framework\Exception\LocalizedException $e) { diff --git a/app/code/Magento/Catalog/Model/Product/Copier.php b/app/code/Magento/Catalog/Model/Product/Copier.php index 44ebdf0f1f283..48429c8476487 100644 --- a/app/code/Magento/Catalog/Model/Product/Copier.php +++ b/app/code/Magento/Catalog/Model/Product/Copier.php @@ -3,10 +3,17 @@ * 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\Catalog\Model\Product\Option\Repository; +use Magento\Catalog\Model\ProductFactory; +use Magento\Framework\App\ObjectManager; +use Magento\Framework\EntityManager\MetadataPool; /** * Catalog product copier. @@ -17,42 +24,49 @@ */ class Copier { - /** - * @var Option\Repository - */ - protected $optionRepository; - /** * @var CopyConstructorInterface */ protected $copyConstructor; /** - * @var \Magento\Catalog\Model\ProductFactory + * @var ProductFactory */ protected $productFactory; /** - * @var \Magento\Framework\EntityManager\MetadataPool + * @var Option\Repository + */ + protected $optionRepository; + + /** + * @var MetadataPool */ protected $metadataPool; /** * @param CopyConstructorInterface $copyConstructor - * @param \Magento\Catalog\Model\ProductFactory $productFactory + * @param ProductFactory $productFactory + * @param ProductRepositoryInterface|null $productRepository + * @param MetadataPool|null $metadataPool */ public function __construct( CopyConstructorInterface $copyConstructor, - \Magento\Catalog\Model\ProductFactory $productFactory + ProductFactory $productFactory, + Repository $optionRepository = null, + MetadataPool $metadataPool = null ) { $this->productFactory = $productFactory; $this->copyConstructor = $copyConstructor; + $this->optionRepository = $optionRepository ?: ObjectManager::getInstance()->get(Repository::class); + $this->metadataPool = $metadataPool ?: ObjectManager::getInstance()->get(MetadataPool::class); } /** * Create product duplicate * - * @param \Magento\Catalog\Model\Product $product + * @param \Magento\Catalog\Model\Product|\Magento\Catalog\Api\Data\ProductInterface $product + * * @return \Magento\Catalog\Model\Product */ public function copy(Product $product) @@ -61,7 +75,7 @@ public function copy(Product $product) $product->getCategoryIds(); /** @var \Magento\Framework\EntityManager\EntityMetadataInterface $metadata */ - $metadata = $this->getMetadataPool()->getMetadata(ProductInterface::class); + $metadata = $this->metadataPool->getMetadata(ProductInterface::class); /** @var \Magento\Catalog\Model\Product $duplicate */ $duplicate = $this->productFactory->create(); @@ -79,11 +93,12 @@ public function copy(Product $product) $this->copyConstructor->build($product, $duplicate); $this->setDefaultUrl($product, $duplicate); $this->setStoresUrl($product, $duplicate); - $this->getOptionRepository()->duplicate($product, $duplicate); + $this->optionRepository->duplicate($product, $duplicate); $product->getResource()->duplicate( $product->getData($metadata->getLinkField()), $duplicate->getData($metadata->getLinkField()) ); + return $duplicate; } @@ -92,7 +107,9 @@ public function copy(Product $product) * * @param Product $product * @param Product $duplicate + * * @return void + * @throws \Exception */ private function setDefaultUrl(Product $product, Product $duplicate) : void { @@ -114,7 +131,9 @@ private function setDefaultUrl(Product $product, Product $duplicate) : void * * @param Product $product * @param Product $duplicate + * * @return void + * @throws \Exception */ private function setStoresUrl(Product $product, Product $duplicate) : void { @@ -158,38 +177,8 @@ private function setStoresUrl(Product $product, Product $duplicate) : void private function modifyUrl(string $urlKey) : string { return preg_match('/(.*)-(\d+)$/', $urlKey, $matches) - ? $matches[1] . '-' . ($matches[2] + 1) - : $urlKey . '-1'; - } - - /** - * Returns product option repository. - * - * @return Option\Repository - * @deprecated 101.0.0 - */ - private function getOptionRepository() - { - if (null === $this->optionRepository) { - $this->optionRepository = \Magento\Framework\App\ObjectManager::getInstance() - ->get(\Magento\Catalog\Model\Product\Option\Repository::class); - } - return $this->optionRepository; - } - - /** - * Returns metadata pool. - * - * @return \Magento\Framework\EntityManager\MetadataPool - * @deprecated 101.0.0 - */ - private function getMetadataPool() - { - if (null === $this->metadataPool) { - $this->metadataPool = \Magento\Framework\App\ObjectManager::getInstance() - ->get(\Magento\Framework\EntityManager\MetadataPool::class); - } - return $this->metadataPool; + ? $matches[1] . '-' . ($matches[2] + 1) + : $urlKey . '-1'; } /** @@ -198,7 +187,7 @@ private function getMetadataPool() * @param array $productData * @return array */ - private function removeStockItem(array $productData) + private function removeStockItem(array $productData) : array { if (isset($productData[ProductInterface::EXTENSION_ATTRIBUTES_KEY])) { $extensionAttributes = $productData[ProductInterface::EXTENSION_ATTRIBUTES_KEY]; @@ -206,6 +195,7 @@ private function removeStockItem(array $productData) $extensionAttributes->setData('stock_item', null); } } + return $productData; } } diff --git a/app/code/Magento/Downloadable/Controller/Adminhtml/Downloadable/Product/Edit/Duplicate.php b/app/code/Magento/Downloadable/Controller/Adminhtml/Downloadable/Product/Edit/Duplicate.php index 3afc55b3d650b..1cf2418ea5d80 100644 --- a/app/code/Magento/Downloadable/Controller/Adminhtml/Downloadable/Product/Edit/Duplicate.php +++ b/app/code/Magento/Downloadable/Controller/Adminhtml/Downloadable/Product/Edit/Duplicate.php @@ -6,6 +6,9 @@ */ namespace Magento\Downloadable\Controller\Adminhtml\Downloadable\Product\Edit; +/** + * @deprecated + */ class Duplicate extends \Magento\Catalog\Controller\Adminhtml\Product\Duplicate { }