How to duplicate a Magento product and add a new SKU with just one save action

I want to duplicate a product and add it sequentially to just one save action.

How can i do this?

I tried this code:

$sku = '123456';
$product = Mage::getModel('catalog/product')
   ->loadByAttribute('sku',$sku); 

$newProduct = $product->duplicate();
$newProduct->setStatus(1);
$newProduct->setSku($sku.'-v2');
$newProduct->save();

but it throws this exception:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '3569-1' for key 'IDX_STOCK_PRODUCT'

Thanks for the help.

+3
source share
1 answer
$newProduct->save();

should be:

$newProduct->getResource()->save($newProduct);
+4
source

All Articles