Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

dupe a product
 
pkircher
Member
 
Avatar
Total Posts:  53
Joined:  2008-09-19
 

u know sometimes its importent u need to duplicate a product codewise ...

it cost me qoute alot to figure the correct scope
here is the code :

<?php
    
require_once 'app/Mage.php';
    
umask(0);
    
/* not Mage::run(); */
    
Mage::app('admin');


    
$product Mage::getModel('catalog/product');
    
$productId $product->getIdBySku("testproduct");
    
$product->load($productId);
                
    
$copy $product->duplicate(); 
    
$id_copy $copy->getId();

    
    
//echo $id_copy;
    
    
$copy Mage::getModel('catalog/product')->load($id_copy); 
    
$copy->setStoreId(Mage::app()->getStore(true)->getId());
    
$copy->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
    
// Apply some changes
    
$copy->setSku("test2"); 
    
$copy->setUrlKey("test2");
    
$copy->setStatus('1'); 

    
// Manage stock
    
$stockData = array();
    
$stockData['qty''1'
    
$stockData['is_in_stock''1'
    
$stockData['manage_stock''0'
    
$stockData['use_config_manage_stock''1'
    
$copy->setStockData($stockData);
    try 
{
    
//Zend_Debug::dump($copy);
    
$copy->save();
    
}catch(Exception $e)
    

        
echo 'Caught exception: ',  $e->getMessage(), "\n";
        
    
}    // Save duplicated product
?>

hopefully it helps somebody

cheers Pierre

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top