Tag Archives: manage product stock programmatically

magento programmatically create product with custom options

We can easily Create custom product with custom options in magento.

First we need to create a custom file in magento root directory.
In this code I am create product.php file in root directory and paste below code.

require_once 'app/Mage.php';
umask(0);
$app = Mage::app();
//$session = Mage::getSingleton('customer/session', array('name'=>'frontend'));

//create a new product
try {

$imageurl = Mage::getBaseDir('media').'/your_img_name.jpg';
$product = Mage::getModel('catalog/product');
$stockData = $product->getStockData();
$stockData['qty'] = 10;
$stockData['is_in_stock'] = 1;
$stockData['manage_stock'] = 1;
$stockData['use_config_manage_stock'] = 0;
$options = array();
$options = array(
'title' => 'Option Title’,
'type’ => 'radio’,
'is_require’ => 1,
'sort_order’ => 0,
'values’ => array()
);
$options['values’][] = array(
'title’ => 'Option Value 1′,
'price’ => 0.00,
'price_type’ => 'fixed’,
'sku’ => '',
'sort_order’ => '1’
);
$options['values’][] = array(
'title’ => 'Option Value 2′,
'price’ => 89.00,
'price_type’ => 'fixed’,
'sku’ => '',
'sort_order’ => '1’
);

$product->setStoreId('default’)
->setTypeId('simple’)
->setAttributeSetId(4)
->setName('GTSProducts’)
->setDescription(“Gts magento test description”)
->setShortDescription(“Gts magento test short description”)
->setSku('gts')
->setWeight(1.0)
->setStatus(1)
->setVisibility(4)
->setPrice(19)
->addImageToMediaGallery($imageurl ,array('thumbnail’,’small_image’,’image’)
,true,false)
->setTaxClassId(0)
->setStockData($stockData)
->setCategoryIds('3′)
->setHasOptions(1)
->setProductOptions(array($options))
->setCanSaveCustomOptions(true)
->save();

echo 'OK Product ID: '.$product->getId();

$cache = Mage::getSingleton('core/cache’);
$cache->flush();
}
catch (Mage_Core_Exception $e) {
echo $e->getMessage();
}
catch (Exception $e) {
echo $e;
}

To know more about magento directory path here

Magento add programatically Product image or addImageToMediaGallery