Magento-Upload bulk product by scripting using CSV file

Magento-UPLOAD BULK PRODUCT BY SCRIPTING USING CSV FILE

In this blog I am going to show you how to upload bulk product in Magento by scripting using csv file (MS excel file). We can directly upload product one by one from admin panel but for a developer it is important to know how we can upload product using csv file in Magento programmatically. We can see these product in “Manage Product” in admin panel.

So, let’s start from the scratch..

What you exactly need to upload bulk product programmatically in Magento-
You need to create csv file (MS excel file). In this file you need to enter all the attribute set details related to products.

Now Go to your Magento root folder: C:/xampp/htdocs/magento/
Copy your csv file (MS excel file) in the root folder (for e.g. productdetails.csv).

Create a php file (for e.g. bulkupload.php) in your magento root folder.
Copy this below code and edit it according to your requirements.

<?php

set_time_limit(0);

ini_set('memory_limit', '1024M');

include_once "app/Mage.php";

include_once "downloader/Maged/Controller.php";

Mage::init();

$app = Mage::app('default');


//The category names should be exactly the same name from the csv file where the id is the corresponding category id in magento. This is done when the csv file doesn't contain ids for categories but the name of categories.


$categories = array(

'Category 1' => 3,

'Category 2' => 4,

'Category 3' =>5,

'Category 4'=>6,

);

$row = 0;

if (($handle = fopen("productdetails.csv", "r")) !== FALSE) {

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

echo 'Importing product: '.$data[0].'<br />';

foreach($data as $d)

{

echo $d.'<br />';

}

$num = count($data);

//echo "<p> $num fields in line $row: <br /></p>\n";

$row++;

if($row == 1) continue;

$product = Mage::getModel('catalog/product');

$product->setSku($data[0]);

$product->setName($data[1]);

$product->setDescription('');

$product->setShortDescription('');

$product->setManufacturer('');

$product->setPrice($data[2]);

$product->setTypeId('simple');

$def_attribute_set = Mage::getModel('catalog/product')->getResource()->getEntityType()->getDefaultAttributeSetId();

$product->setAttributeSetId($def_attribute_set); // need to look this up

$product->setCategoryIds(array($categories[$data[4]])); // need to look these up

$product->setWeight(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()));

$stockData = $product->getStockData();

$stockData['qty'] = $data[18]; //18

$stockData['is_in_stock'] = 1;

$stockData['manage_stock'] = 1;

$stockData['use_config_manage_stock'] = 0;

$product->setStockData($stockData);

$product->save();

}

fclose($handle);

}

?>

*Save the changes and do not forget to clear Magento Cache.