Creating Product Programmatically in Magento 1.7 – Guide

magento-1.7

I am trying to create a product progrmatically in magento

require_once('./app/Mage.php');



Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model‌​_App::ADMIN_STORE_ID));
$product = Mage::getModel('catalog/product');

$product->setSku("ABC123") // OK
    ->setName("Type 7 Widget") // NOK
    ->setDescription("This widget will give you years of trouble-free widgeting.") // NOK
    ->setShortDescription("High-end widget.") // NOK
    ->setPrice(70.50) // OK
    ->setTypeId('simple') // OK
    ->setAttributeSetId('14') // need to look this up // OK
    ->setCategoryIds("3,7") // need to look these up // OK
    ->setWeight(1.0) // OK
    ->setTaxClassId(1) //taxable goods // ?
    ->setVisibility(4) // catalog, search // NOK
    ->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED); // enabled // NOK

$product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));

$product->save();

?>

here is my code . it is giving me error
Fatal error: Class 'Mage_Core_Model‌​_App'

Can anybody tell me wh

Best Answer

Always better to share code:

<?php
//$product = Mage::getModel('catalog/product');
$product = new Mage_Catalog_Model_Product();
// Build the product
$product->setSku('some-sku-value-here');
$product->setAttributeSetId('some_int_value_of_some_attribute');
$product->setTypeId('simple');
$product->setName('Some cool product name');
$product->setCategoryIds(array(7)); # some cat id's, my is 7
$product->setWebsiteIDs(array(1)); # Website id, my is 1 (default frontend)
$product->setDescription('Full description here');
$product->setShortDescription('Short description here');
$product->setPrice(39.99); # Set some price
# Custom created and assigned attributes
$product->setHeight('my_custom_attribute1_val');
$product->setWidth('my_custom_attribute2_val');
$product->setDepth('my_custom_attribute3_val');
$product->setType('my_custom_attribute4_val');
//Default Magento attribute
$product->setWeight(4.0000);
$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
$product->setStatus(1);
$product->setTaxClassId(0); # My default tax class
$product->setStockData(array(
    'is_in_stock' => 1,
    'qty' => 99999
));
$product->setCreatedAt(strtotime('now'));
try {
    $product->save();
}
catch (Exception $ex) {
    //Handle the error
}
?>