1

this is the code I am using, it returns a server error. I am not sure why or what to do for it to work. any help would be greatly appreciated.

<?php
//product = Mage::getModel('catalog/product');

error_reporting(E_ALL | E_STRICT);
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
umask(0);
Mage::app();
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));


$product = new Mage_Catalog_Model_Product();
    $product->setSku("ABC123");
    $product->setName("Type 7 Widget");
    $product->setDescription("This widget will give you years of trouble-free widgeting.");
    $product->setShortDescription("High-end widget.");
    $product->setPrice(70.50);
    $product->setTypeId('simple');
    $product->setAttributeSetId(9); // need to look this up
    $product->setCategoryIds("20,24"); // need to look these up
    $product->setWeight(1.0);
    $product->setTaxClassId(2); // taxable goods
    $product->setVisibility(4); // catalog, search
    $product->setStatus(1); // enabled

    // assign product to the default website
    $product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));

    $product->save();

// for stock

$stockData = $product->getStockData();
$stockData['qty'] = 10;
$stockData['is_in_stock'] = 1;
$stockData['manage_stock'] = 1;
$stockData['use_config_manage_stock'] = 0;
$product->setStockData($stockData);
?>
1
  • what error are you getting? make sure error reporting is on.. Commented May 30, 2013 at 11:21

1 Answer 1

1

With Magento we can have several different ways to add products to the catalog. The way you are try to achieve need a few changes to work well. Please keep in mind that you should use as much of the Magento's functionalities, such as models, configs and so on.

A good approach for what you are looking for would be:

<?php 
...

$product = Mage::getModel('catalog/product');

$product->setSku("SKUPROD123");
$product->setName("Name of The Product");
$product->setDescription("Some description of the product.");
$product->setShortDescription("Short one.");
$product->setPrice(299.50);
$product->setTypeId('simple');
$product->setAttributeSetId(9); // need to look this up
$product->setCategoryIds("20,24"); // need to look these up
$product->setWeight(1.8);
$product->setTaxClassId(2); // taxable goods
$product->setVisibility(4); // sets visibility as 'catalog, search'
$product->setStatus(1); // enabled

// assign product to the default website
$product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));

$product->save();

I hope it helps

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.