We have written code to create product. Once a new product is created a duplicate of newly created product is generated. the code is as follows.

public function enableMasterProduct($product_code, $product_id){            

        $product = Mage::getModel ('catalog/product')->load($product_id);                           

        $masterProducts = Mage::getModel ('catalog/product')
                            ->addAttributeToFilter ( 'is_master_record', 1 )
                            ->addAttributeToFilter ( 'productcode', $product_code); 

        $pCount = $masterProducts->getSize ();

        if ($pCount == 0) {
            $newProduct = $product->duplicate();
            $newProduct->setData('group_price',array ());
            $id = $newProduct->getId();         
            $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($id);
            if ($stockItem->getId() > 0 and $stockItem->getManageStock()) {
                $qty = 1;
                $stockItem->setIsInStock((int)($qty > 0));


This code works and a new product is generated. However the product image generated twice even though there is only one product image.

1 Answer 1


You have to clear media gallery before save:


Your Answer

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

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