Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load product in default store before duplicating #23209

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
*/
namespace Magento\Bundle\Controller\Adminhtml\Bundle\Product\Edit;

/**
* @deprecated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add description why it became deprecated and add @see tag with example what we should use instead

*/
class Duplicate extends \Magento\Catalog\Controller\Adminhtml\Product\Duplicate
{
}
1 change: 1 addition & 0 deletions app/code/Magento/Catalog/Block/Adminhtml/Product/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ public function getProductSetId()

/**
* @return string
* @deprecated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add description why it became deprecated and add @see tag with example what we should use instead

*/
public function getDuplicateUrl()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Magento\Backend\App\Action;
use Magento\Catalog\Controller\Adminhtml\Product;

/**
* @deprecated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add description why it became deprecated and add @see tag with example what we should use instead

*/
class Duplicate extends \Magento\Catalog\Controller\Adminhtml\Product
{
/**
Expand Down
16 changes: 13 additions & 3 deletions app/code/Magento/Catalog/Controller/Adminhtml/Product/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
80 changes: 35 additions & 45 deletions app/code/Magento/Catalog/Model/Product/Copier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand All @@ -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();
Expand All @@ -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;
}

Expand All @@ -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
{
Expand All @@ -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
{
Expand Down Expand Up @@ -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';
}

/**
Expand All @@ -198,14 +187,15 @@ 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];
if (null !== $extensionAttributes->getStockItem()) {
$extensionAttributes->setData('stock_item', null);
}
}

return $productData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
*/
namespace Magento\Downloadable\Controller\Adminhtml\Downloadable\Product\Edit;

/**
* @deprecated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add description why it became deprecated and add @see tag with example what we should use instead

*/
class Duplicate extends \Magento\Catalog\Controller\Adminhtml\Product\Duplicate
{
}