Creating Magento products from a script
07 Oct 2009, by Peter Spiller
8 Comments · Posted in Magento

    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 save() method:

    $product = Mage::getModel('catalog/product');
    $product->setName("Type 7 Widget");
    $product->setDescription("This widget will give you years of trouble-free widgeting.");
    $product->setShortDescription("High-end widget.");
    $product->setAttributeSetId(9); // need to look this up
    $product->setCategoryIds("20,24"); // need to look these up
    $product->setTaxClassId(2); // taxable goods
    $product->setVisibility(4); // catalog, search
    $product->setStatus(1); // enabled
    // assign product to the default website

    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.

    First of all, you need to make sure you set the product's visibility to something that includes 'catalog' (2 is catalog only, 4 is catalog and search results) and the product's status must be set to enabled (1). If you want the product to appear in one or more categories (which you usually do), the product's category IDs must be set correctly. Note that the setCategoryIds() function takes a comma-separated string of category IDs, not an array. A category's ID can be found by viewing it in the Manage Categories screen on the backend. The product's attribute set ID should also be defined. You can get attribute set IDs by checking the URLs in Manage Attribute Sets (i.e. if the link to edit the 'Default' attribute set is admin/catalog_product_set/edit/id/4/, then its ID is 4).

    Even once all these settings are complete, the product will almost certainly not appear on the frontend. One other attribute must be set - the websites the product belongs to. This even has to be set if your Magento installation doesn't use multiple websites. The setWebsiteIds() line above assigns the product to the default website, which is sufficient for a single-website setup. Note that this method uses an array, unlike setCategoryIds().

    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 getModel() and 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;

    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.

    Creating products from a script is quite easy, and using the internal Magento API can be faster and a lot more flexible than using the web services Magento Core API or an import profile and CSV file.