2

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')
                            ->getCollection()
                            ->addAttributeToFilter ( 'is_master_record', 1 )
                            ->addAttributeToFilter ( 'productcode', $product_code); 

        $pCount = $masterProducts->getSize ();

        if ($pCount == 0) {
            $newProduct = $product->duplicate();
            $newProduct->setStatus(2);
            $newProduct->setSku('master_'.uniqid());
            $newProduct->setData('group_price',array ());
            $newProduct->setIsMasterRecord(1);
            $newProduct->setSellerId(0);                        
            $newProduct->setProductcode($product_code);
            $id = $newProduct->getId();         
            $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($id);
            if ($stockItem->getId() > 0 and $stockItem->getManageStock()) {
                $qty = 1;
                $stockItem->setQty($qty);
                $stockItem->setIsInStock((int)($qty > 0));
                $stockItem->save();
            }
            $newProduct->getResource()->save($newProduct);
        }

    }

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

2

You have to clear media gallery before save:

$newProduct->setMediaGallery(array());

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.