Magento allows several different ways to add products to the catalog, including manual editing, upload via spreadsheet and the web services Magento Core API. For developers who want to add products through PHP however, there are a few caveats to be aware of for the product to be displayed on the frontend.
The basics of adding a new product are trivial – you get a new copy of the appropriate model, fill in the details and call the
$product = Mage::getModel('catalog/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();
The example above shows a minimal set of attributes you should be setting on a new product. If you run this code and look under Manage Products on the backend, you should see your new product. However, the real trick is making sure they appear on the frontend.
The product should now be appearing on the frontend, but it will probably be showing up as ‘Out of stock’. A product’s inventory and stock options are contained in a separate object, rather than as part of the product itself. Adding the following code somewhere between the
save() lines will allow you to set the stock data:
$stockData = $product->getStockData(); $stockData['qty'] = 10; $stockData['is_in_stock'] = 1; $stockData['manage_stock'] = 1; $stockData['use_config_manage_stock'] = 0; $product->setStockData($stockData);
This code gets the product’s stock and inventory options as an array, modifies them and then saves the array back to the object. The options should be self-explanatory – the important one is ‘is_in_stock’, which controls whether the product is in stock or not. The example above sets the product’s ‘Manage Stock’ attribute to ‘Yes’ and un-ticks the corresponding ‘Use Config Settings’ checkbox, so the product will always track stock regardless of the global config setting. This is just an example, so be sure to set values appropriately for your store.