Adding a new product

Last modified by Bojan Hrnkas on Fri, June 25, 2010 09:17
Source|Old Revisions  

This is an old revision of the document!

Here is how you can add a product using Magento backend.

//create a new product
try {
  $newProduct = Mage::getModel('catalog/product')
    ->setName('4You T Shirt')
  echo 'OK Product ID: '.$newProduct->getId();
catch (Mage_Core_Exception $e) {
  echo $e->getMessage();
catch (Exception $e) {
  echo $e;

You can set any other attribute by using universal setter function setData($attribute_code, $value) or for example for shirt size: setShirtSize($value). Varian object translates it automatically into setData(’shirt_size’, $value).

If you want to update existing product, call $newProduct→load($id) before setting any attributes.