Creating magento products on-the-fly


 <?php
require_once 'app/Mage.php';
Mage::app();
// instatiate Product
$product = Mage::getModel('catalog/product');

$product->setWebsiteIds(array(1));
$product->setSku('rand-sku-' . rand());
$product->setPrice(rand(100,2000)); 
$product->setAttributeSetId(4);
$product->setCategoryIds(array(3));
$product->setType('Simple Product');
$product->setName('Product Name'.rand(1,200000));
$product->setDescription('The Product Description');
$product->setShortDescription('Brief Description');
$product->setStatus(1);
$product->setTaxClassId('2');
$product->setWeight(0);
$product->setCreatedAt(strtotime('now'));

/* ADDITIONAL OPTIONS

   $product->setCost();
   $product->setInDepth();
   $product->setKeywords();

*/

$product->save();

// "Stock Item" still required regardless of whether inventory
// control is used, or stock item error given at checkout!

$stockItem = Mage::getModel('cataloginventory/stock_item');
$stockItem->loadByProduct($product->getId());
$stockItem->setData('is_in_stock', 1);
$stockItem->save();
header("Location: /checkout/cart/add/product/".$product->getId()."/");
?>

Special thanks to :
http://magentoexpert.co.uk/2009/02/10/creating-magento-products-on-the-fly/

Share on Facebook




About Pragnesh Karia

Pragnesh Karia, Open Source Enthusiast, Software Professional, Software Developer, Technical Lead ,Magento, Joomla ,Joomla LMS , Moodel LMS ,PHP ,Mysql, Ajax, Javascript, Jquery, Linux, Fan of Open Sources , Annet Technologies , SEO Analyst , Mootools