Create simple product magento

Import simple products

This section explains the how to create simple products using magento script.

This function create simple products pro grammatically. The function accept ( $import_product )- array contains the products details read from the CSV , sku, visibility, category array, trier price details and the image folder directory path as the parameters as shown below.

function saveSingleProduct($import_product,$sku,$enabled,$visible,$migrated_categories,$discount_arr,$dir) {
    
     try {
        $tierPrices = array();
        $product = Mage::getModel('catalog/product');
      
        $product->setWebsiteIds(array(1)) //website ID the product is assigned to, as an array
                ->setAttributeSetId(4)  //ID of a attribute set named 'default'
                ->setTypeId('simple') //product type
                ->setCreatedAt(strtotime('now')) //product creation time
                ->setSku($sku) //SKU
                ->setName($import_product[1]) //product name
                ->setWeight($import_product[16])
                ->setStatus($enabled) //product status (1 - enabled, 2 - disabled)
                ->setTaxClassId(0) //tax class (0 - none, 1 - default, 2 - taxable, 4 - shipping)
                ->setVisibility($visible)//catalog and search visibility
                ->setPrice($import_product[11])//price in form 11.22
                ->setCost(1)//price in form 11.22              
                ->setMetaTitle($import_product[1])
                ->setMetaKeyword($import_product[26])
                ->setMetaDescription($import_product[28])
                ->setDescription($import_product[27])
                ->setShortDescription($import_product[27])
                ->setStockData(array(
                        'use_config_manage_stock' => 1, //'Use config settings' checkbox
                        'is_in_stock' => 1, //Stock Availability
                        'qty' => (int)$import_product[6] //qty
                    )
                )
                ->setCategoryIds( $migrated_categories); //assign product to categories
        
     
        if ($product->save()){ //($product->save()) {
            echo 'Single Product ' . $product->getName() . ' ' . $product->getId() . ' imported successfully' . PHP_EOL;
        }
          
        } catch (Exception $e) {
            Mage::log($e->getMessage());
        }
        return array($import_product[0],$product->getId(), $import_product[33]);
}

Share this Tutorial