Here is a working sample of code for programatically (manually) creating simple Magento product from within your custom 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 } ?>
Although the process seems pretty straight forward, keep an eye on setStockData method. I was getting some foreign key constraint errors when trying to create instance of stock item then save it into the product object. The above code should handle it quite nicely. Tested on latest Magento version, which is 1.3.2.3 as of time of this writing.