0

Magento 2.4.6

I am trying to duplicate a product using the \Magento\Catalog\Model\Product\Copier class which is also used in Magento\Catalog\Controller\Adminhtml\Product\Duplicate in the execute function..

When I click the button in admin to duplicate a product, it works, all attributes are being set in the copy. Doing the same in my code does duplicate the product, but not all attributes are set.

My Code:

$original = $objectManager->create('Magento\Catalog\Model\Product')->load(1234567);
$copy = $objectManager->create('\Magento\Catalog\Model\Product\Copier')->copy($original);

This creates a copy of the product, but most of the attributes are not set like in the original. Do I have to do something more?

Thanks!

1
  • Use repository instead of model class
    – Sohel Rana
    Commented Jun 13, 2023 at 10:51

1 Answer 1

1

Try this

In your code, you're using the Magento\Catalog\Model\Product\Copier class to duplicate a product, similar to how it's done in the execute function of the Magento\Catalog\Controller\Adminhtml\Product\Duplicate class. However, you're noticing that not all attributes are being set in the copied product.

When duplicating a product, there are several steps involved that ensure all attributes are properly copied. In your code snippet, you're only using the copy method from the Magento\Catalog\Model\Product\Copier class, which may not handle all the necessary steps for attribute duplication.

1.Retrieve the original product:

Instead of using the object manager directly, it's recommended to use dependency injection to retrieve the product model. However, for the sake of demonstration, I'll use the object manager in this example. Replace 1234567 with the ID of the product you want to duplicate.

 $original = $objectManager->create('Magento\Catalog\Model\Product')->load(1234567);

2.Create a new product object and copy attributes: Instead of relying solely on the copy method, you can manually copy the attributes from the original product to the new product object. This ensures all relevant attributes are set in the copied product.

 $copy = $objectManager->create('Magento\Catalog\Model\Product');
 $copy->setData($original->getData());

3.Clear specific attributes:

There might be some attributes that should not be copied or need to be cleared in the copied product. For example, you might want to generate a new SKU or clear certain values. You can do this by accessing the specific attributes on the copied product and resetting their values.

 $copy->setSku('new_sku'); // Generate a new SKU
 $copy->setData('custom_attribute', null); // Clear a specific attribute

4.Save the copied product:

Finally, save the copied product using the appropriate mechanism, such as a repository or service contract, instead of relying on the object manager directly.

$productRepository = $objectManager->create('Magento\Catalog\Api\ProductRepositoryInterface');
$productRepository->save($copy);

It's recommended to use dependency injection to retrieve the product repository instead of using the object manager directly.

By following these steps, you should be able to properly duplicate a product and ensure that all relevant attributes are set in the copied product.

     *HOPE YOU IT CAN USEFUL TO YOU*
1
  • Thanks, this works, had to set the id to null ($copy-setId(null);), otherwise it did not copy, but overwrite. Now it copies, but the images are missing, the images were there using the Copier though :-D ... any ideas on this? Commented Jun 14, 2023 at 6:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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