Monday, 16 July 2012

Magento product add programmatically


CID= Category ID
Name= Product name
sku = Product Sku
weight = Product Weight.


<?php $data=array("weight"=>"10","sku"=>"sku20","name"=>"productnew","description"=>"description","sdesc"=>"sdesc","price"=>"103","cid"=>"4"); ?>
<?php $newproduct = new Mage_Catalog_Model_Product();
$newproduct->setTypeId('simple');
$newproduct->setWeight($data['weight']);
$newproduct->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
$newproduct->setStatus(1);
$newproduct->setSku($data['sku']);
$newproduct->setTaxClassId(0);
$newproduct->setCategoryIds($data['cid']);

$newproduct->setWebsiteIDs(array(1));
$newproduct->setStoreIDs(array(1));
$newproduct->setStockData(array(
'is_in_stock' => 1,
'qty' => 199,
'manage_stock' => 1
));
$newproduct->setAttributeSetId(4);
$newproduct->setName($data['name']);
//$newproduct->setCategoryIds(array(3)); // array of categories it will relate to
$newproduct->setDescription($data['description']);
$newproduct->setShortDescription($data['sdesc']);
$newproduct->setPrice($data['price']);
//$newproduct->save();
?>
<?php
$fullImagePath="/opt/lampp/htdocs/wishlists/skin/frontend/default/default/images/a3.jpg";
$newproduct->setMediaGallery (array('images'=>array (), 'values'=>array ()));
$newproduct->addImageToMediaGallery ($fullImagePath, array ('image','small_image','thumbnail'), false, false);
//$newproduct->addImageToMediaGallery ($fullImagePath, array ('small_image'), false, false);
//$newproduct->addImageToMediaGallery ($fullImagePath, array ('thumbnail'), false, false);
$newproduct->save();
?>

No comments:

Post a Comment