I want to duplicate product using product id programmatically. I have checked it but not found any useful solution for it.

Basically, in product details page one button near by "Add to cart" button and when I click it then current product duplicate in store. If product is configure that selected attributes associate product duplicate.

I got associate product Id when I click on button. Also I create simple product using Product id But problem is i can't Add some static attribute. I got only selected attributes value.

I want to clone product of selected associate product. Also after clone I want to change quantity as well as sku.

How to do it?

  • you got any solution? – Magento2 Devloper Apr 7 '18 at 19:29
  • Yes, but with custom code. Everything is working it. But now facing a problem with the custom option. – nil108 Apr 9 '18 at 9:21
  • can you share your code please so I can check it – Magento2 Devloper Apr 9 '18 at 9:36

Here is the code i used to create duplicate product:

$oldSku = $_GET['oldp'];
$newSku = $_GET['newp'];

if(!$oldSku || !$newSku){die('missing argument');}

$productFactory = $objectManager->create('Magento\Catalog\Model\ProductFactory');

$oldProduct = $productFactory->create()->loadByAttribute('sku',$oldSku);
$websiteIds = $oldProduct->getWebsiteIds();
$categoryIds = $oldProduct->getCategoryIds();

$duplicate = null;

$duplicate = $productFactory->create();



$urlKey = $newSku;
$urlKey = preg_match('/(.*)-(\d+)$/', $urlKey, $matches) ? $matches[1] . '-' . ($matches[2] + 1) : $urlKey . '';




                'use_config_manage_stock' => 0,
                'manage_stock' => 1,
                'is_in_stock' => 0,
                'qty' => 0

  • I need also duplicate image set with image role. I have used $_product->addImageToMediaGallery($image_path, array( 'thumbnail','small_image','image' ), false, false); but Image Role not set on backend. – nil108 Mar 8 '18 at 9:26
  • @nil108 Did you get any luck in duplicating the images? – Mehdi Apr 23 '18 at 19:00

Take a look at \Magento\Catalog\Controller\Adminhtml\Product\Duplicate and then especially the Product Copier that is used (\Magento\Catalog\Model\Product\Copier).

It is used in the admin for duplicating products, but we used it in the past to programatically duplicate products with a new SKU and QTY. It might fit your needs.

  • I have used this and I am able to successfully duplicate the product, however, the duplicated product is missing images. Any thoughts on this? – Mehdi Apr 23 '18 at 18:58

This is fast way to duplicate the product by id using object method :

//get object method
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

// load product by id
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($product_id);

//duplicate product
$productCopier = $objectManager->create('\Magento\Catalog\Model\Product\Copier')->copy($product);

Or by __construct :

 * @var \Magento\Catalog\Model\Product
protected $product;
 * @var \Magento\Catalog\Model\Product\Copier
protected $productCopier;

public function __construct(
    \Magento\Catalog\Model\Product $product
    \Magento\Catalog\Model\Product\Copier $copier
) {        
    $this->product = $product;
    $this->productCopier = $copier; 
// load product by id
$product = $this->product->load($product_id);

//duplicate product
$productCopier = $this->productCopier->copy($product);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.