Magento create product programmatically


Magento create product programmatically : It’s very common to create products in magento from admin panel but sometimes we need to create products programatically for some specific requirement. You can create products programatically and assign values dynamically as per your requirement. Here we are going to learn how to create simple product in magento programatically.


Magento create product programmatically

You can create products programmatically as below-

Magento create product programmatically:

public function createProduct(){
try{
    // first set current store
    Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
    // load catalog produuct model
    $product = Mage::getModel('catalog/product');
    $product->setAttributeSetId(9)
               ->setTypeId('simple')
               ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
               ->setTaxClassId(2)
               ->setCreatedAt(strtotime('now'))
               ->setName("Test Product")
               ->setSku("test-sku")
               ->setWeight(50)
               ->setStatus(1)
               ->setPrice(90)
               ->setCategoryIds(array(1,4,8))
               ->setWebsiteIds(array(1,6))
               ->setDescription("Test Description")
               ->setShortDescription("Test Short Description")
               ->setFreeGroundShipping(50)
               ->setMetaTitle("Test")
               ->setMetaKeyword("Test")
               ->setMetaDescription("Test")
               ->setStockData(array(
                                     'manage_stock'=>0,
                                     'min_sale_qty'=>1,
                                     'max_sale_qty'=>3,
                                     'is_in_stock'=>1))
               ->setSetupFee(20)
               ->setsetupCost(10);
    $product->save();                
}catch(Exception $e){
Mage::log("Exception ---". $e->getMessage());
}

}

The above function will create simple product in magento which is decided by type id. You can add categories, price, name sku and quantity etc while creating product


Advertisements

Add Comment

📖 Read More