For Import Product Pragmatically In Magento (Simple CSV) 2
<?php
set_time_limit(0);
ini_set(‘max_execution_time’, 0);
ini_set(‘memory_limit’, ‘-1’);
include_once “app/Mage.php”;
//include_once “downloader/Mage/Controller.php”;
Mage::init();
$app = Mage::app(‘default’);
$row = 0;
if (($handle = fopen(“products/bakery-dairy/test.csv”, “r”)) !== FALSE) {
while (($data = fgetcsv($handle, 1000, “,”)) !== FALSE) {
if($data[0] !=’product_image’){
// echo ‘Importing product: ‘.$data[0].'<br />’;
foreach($data as $d)
{
//echo $d.'<br />’;
}
$num = count($data);
$row++;
//if($row == 1) continue;
$product = Mage::getModel(‘catalog/product’);
$sku = str_replace(‘ ‘,’_’,strtolower($data[1])).$data[15].rand();
$sortdescription = $data[1].’ ‘.$data[10].’ ‘.$data[11].’ ‘.$data[13] ;
$product->setSku($sku);
$product->setName($data[1]);
$product->setDescription($data[4]);
$product->setShortDescription($sortdescription);
$product->setBrand($data[15]);
$product->setPrice($data[17]);
$product->setTypeId(‘simple’);
$image_url = $data[0]; //get external image url from csv
$image_type = substr(strrchr($image_url,”.”),1); //find the image extension
$filename = md5($image_url . $sku).’.’.$image_type; //give a new name, you can modify as per your requirement
$filepath = Mage::getBaseDir(‘media’) . DS . ‘import’. DS . $filename; //path for temp storage folder: ./media/import/
file_put_contents($filepath, file_get_contents(trim($image_url))); //store the image from external url to the temp storage folder
$mediaAttribute = array (
‘thumbnail’,
‘small_image’,
‘image’
);
$product->addImageToMediaGallery($filepath, ‘thumbnail’, false);
$product->addImageToMediaGallery($filepath, ‘small_image’, false);
$product->addImageToMediaGallery($filepath, ‘image’, false);
$product->setAttributeSetId(4); // need to look this up
$product->setCategoryIds(array(60, 131)); // 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()));
//$weight = array(’25gm’,’50gm’,’75gm’,’100gm’);
$weight = explode(‘;’,$data[11]);
if(count(array_filter($weight)) > 0){
$options = array();
$optionData = array();
for($i = 0; $i < count($weight); $i++){
$options[$i][‘is_delete’] = ”;
$options[$i][‘title’] = trim($weight[$i]);
$options[$i][‘price_type’] = ‘fixed’;
$options[$i][‘price’] = ($i*$data[17]) – ($i/10*$data[17]) ;
$options[$i][‘sku’] = ”;
}
$optionData = array(
‘is_delete’ => 0,
‘is_require’ => true,
‘previous_group’ => ”,
‘title’ => ‘Weight:’,
‘type’ => ‘radio’,
‘values’ => $options
);
$optionInstance = $product->getOptionInstance()->unsetOptions();
$product->setHasOptions(1);
$optionInstance->addOption($optionData);
$optionInstance->setProduct($product);
}
$product->save();
$stockItem = Mage::getModel(‘cataloginventory/stock_item’);
$stockItem->assignProduct($product);
$stockItem->setData(‘is_in_stock’, 1);
$stockItem->setData(‘stock_id’, 1);
$stockItem->setData(‘store_id’, 1);
$stockItem->setData(‘manage_stock’, 1);
$stockItem->setData(‘use_config_manage_stock’, 0);
$stockItem->setData(‘min_sale_qty’, 1);
$stockItem->setData(‘use_config_min_sale_qty’, 0);
$stockItem->setData(‘max_sale_qty’, 1000);
$stockItem->setData(‘use_config_max_sale_qty’, 0);
$stockItem->setData(‘qty’, 10);
$stockItem->save();
echo ‘Last Inserted Product ID is : ‘.$product->getId();
}
}
fclose($handle);
}
?>
We are glad that you preferred to contact us. Please fill our short form and one of our friendly team members will contact you back.