Categories
Magento

Create simple Magento product programmatically

Magento has a great backend to add different types of product such as configurable, bundle, etc. But there are cases when you need to import products into Magento programmatically or manually. For example, you have a list of products in CSV format and you need to bulk import all of those products.

How to create simple products programmatically (or manually)

  1. Simply create a new file in the root of Magento installation and named it “importer.php”
  2. Add the following lines of code and change it as per your requirement.
    require '../app/Mage.php'; Mage::app(); Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); $product = Mage::getModel('catalog/product'); try {     $product         ->setStoreId(1)//you can set data in store scope         ->setWebsiteIds(array(1))//website ID the product is assigned to, as an array         ->setAttributeSetId(19)//ID of a attribute set named 'default'         ->setTypeId('simple')//product type         ->setCreatedAt(strtotime('now'))//product creation time         ->setUpdatedAt(strtotime('now'))//product update time         ->setSku('musk')//SKU         ->setName('musk jacket')//product name         ->setWeight(1.0000)         ->setStatus(1)//product status (1 - enabled, 2 - disabled)         ->setTaxClassId(4)//tax class (0 - none, 1 - default, 2 - taxable, 4 - shipping)         ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)//catalog and search visibility         ->setNewsFromDate('02/14/2017')//product set as new from         ->setNewsToDate('02/14/2017')//product set as new to         ->setCountryOfManufacture('US')//country of manufacture (2-letter country code)         ->setPrice(100.00)         ->setDescription('This is a long description')         ->setShortDescription('This is a short description')         ->setMediaGallery(array('images' => array(), 'values' => array()))         ->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'importer' . DS.'image.jpg', array('image', 'thumbnail', 'small_image'),             false, false)         ->setStockData(array(                 'use_config_manage_stock' => 0, //'Use config settings' checkbox                 'manage_stock'            => 1, //manage stock                 'min_sale_qty'            => 1, //Minimum Qty Allowed in Shopping Cart                 'max_sale_qty'            => 2, //Maximum Qty Allowed in Shopping Cart                 'is_in_stock'             => 1, //Stock Availability                 'qty'                     => 999 //qty             )         )         ->setCategoryIds(array(1, 2, 3, 4)); //assign product to categories     $product->save(); } catch (Exception $e) {     Mage::log($e->getMessage()); } $product->save();

Code Explanation

Firstly, we need to set some mandatory fields.

require '../app/Mage.php';
Mage::app();
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product = Mage::getModel('catalog/product');
try {
    $product
        ->setStoreId(1)//you can set data in store scope
        ->setWebsiteIds(array(1))//website ID the product is assigned to, as an array
        ->setAttributeSetId(19)//ID of a attribute set named 'default'
        ->setTypeId('simple')//product type

Secondly, we need to make the product visible simply by adding the following set of codes:

$product
        ->setStatus(1)//product status (1 - enabled, 2 - disabled)
        ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)//catalog and search visibility
        ->setCategoryIds(array(1, 2, 3, 4)); //assign product to categories

Next, importing images in the product.

$product
        ->setMediaGallery(array('images' => array(), 'values' => array()))//media gallery initialization
        ->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'importer' . DS.'image.jpg', array('image', 'thumbnail', 'small_image'),
            false, false)

After that, configuring stock:

$product
        ->setStockData(array(
                'use_config_manage_stock' => 0, //'Use config settings' checkbox
                'manage_stock'            => 1, //manage stock
                'min_sale_qty'            => 1, //Minimum Qty Allowed in Shopping Cart
                'max_sale_qty'            => 2, //Maximum Qty Allowed in Shopping Cart
                'is_in_stock'             => 1, //Stock Availability
                'qty'                     => 999 //qty
            )
        )

Finally, set mandatory attributes such as name, price and description.

$product
        ->setSku('musk')//SKU
        ->setName('musk jacket')//product name
        ->setWeight(1.0000)
        ->setTaxClassId(4)//tax class (0 - none, 1 - default, 2 - taxable, 4 - shipping)
       ->setPrice(100.00)
        ->setDescription('This is a long description')
        ->setShortDescription('This is a short description')

Just add the following line to save your product.

$product->save();

P.S. above code is only tested on Magento 1.9.*.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.