0

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

4
  • try to add $product->setStockData(array('is_in_stock' => 1, 'manage_stock' => 1)); Commented Jun 13, 2014 at 8:14
  • Where to add this code? @Mohit Kumar Commented Dec 1, 2016 at 11:50
  • @JulienLachal Where to add the above code? Commented Dec 1, 2016 at 11:53
  • It's located in a standalone script. Commented Dec 1, 2016 at 12:39

2 Answers 2

3

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
}
?>
1
  • Hello, Where to add this code? @TBI Infotech Commented Dec 1, 2016 at 11:49
0

The code you require is in this link

http://inchoo.net/ecommerce/magento/programming-magento/programatically-manually-creating-simple-magento-product/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.