cancel
Showing results for 
Search instead for 
Did you mean: 

Programatically upload a Bulk simple products

Programatically upload a Bulk simple products

 

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$productArr = [];
$tmpFilename = $_FILES["docname"]["tmp_name"];
// $file = 'var/importexport/bulkdynamic.csv';
$csv = new Varien_File_Csv();
$data = $csv->getData($tmpFilename);
$rows = count($data[0]);
$attributeCode = array();
$attributeCodewithId = array();
for ($j = 16; $j<= $rows; $j++) {

$cols = $data[0][$j];
if(!empty($cols)){
$attributeCode[] = $cols;
}
}
foreach($attributeCode as $code){

$attribute_details = Mage::getSingleton("eav/config")->getAttribute('catalog_product', $code);
$attribute = $attribute_details->getData();
$attribute_id = $attribute['attribute_id'];
if($attribute_id){
$attributeCodewithId[$code] = $attribute_id ;
}else{
echo "Error : ".$code."is not Valid";
break;
exit;
}


}
foreach ($data as $value) {

$cat = Mage::helper('bulkupload')->getCategoryNameById($value[3]);
$websiteId = Mage::app()->getWebsite()->getId();
if($value[1]=='simple'){
$simpleProduct = Mage::getModel('catalog/product');
try {


$simpleProduct
->setWebsiteIds(array($websiteId)) //website ID the product is assigned to, as an array
->setAttributeSetId($value[15]) //ID of a attribute set named 'default'
->setTypeId($value[1]) //product type
->setCreatedAt(strtotime('now')) //product creation time
->setSku($value[0]) //SKU
->setName($value[2]) //product name
->setWeight($value[6])
->setStatus(1) //product status (1 - enabled, 2 - disabled)
->setTaxClassId(0) //tax class (0 - none, 1 - default, 2 - taxable, 4 - shipping)
->setVisibility(1); //not visible visibility
$cnt = 16;
$optionLabelArr = array();
foreach($attributeCode as $code){
$set = 'set'.$code;
$optionLabelArr[] = $value[$cnt];
$optionId = Mage::helper('bulkupload')->getOptionId($code,$value[$cnt]);
$simpleProduct->$set($optionId);
$cnt++;
}

$simpleProduct->setNewsFromDate('') //product set as new from
->setNewsToDate('') //product set as new to
->setPrice($value[7]) //price in form 11.22
->setSpecialPrice('') //special price in form 11.22
->setSpecialFromDate('') //special price from (MM-DD-YYYY)
->setSpecialToDate('') //special price to (MM-DD-YYYY)
->setMetaTitle($value[8])
->setMetaKeyword($value[10])
->setMetaDescription($value[9])
->setDescription($value[4])
->setShortDescription($value[5]);
if(!empty($value[14])){
$galleryData = explode(',',$value[14]);
$simpleProduct->setMediaGallery(array('images'=>array (), 'values'=>array ()));
foreach($galleryData as $gallery_img) {
if ($gallery_img){
$simpleProduct->addImageToMediaGallery($gallery_img, array ('image','small_image','thumbnail'), false, false);
}
else{
$err = 1;
Mage::getSingleton('core/session')->addError($this->__('Image Path of '.$value[0].' Not Exist !!!'));
}
}
}
$simpleProduct->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' => $value[11] //qty
)
)
->setCategoryIds(array($cat));
$simpleProduct->save();
echo "SKU:".$value[0].'&nbsp;added sucessfully'."<br />";
} catch (Exception $e) {
Mage::log($e->getMessage());
//echo $e->getMessage();
echo "SKU:".$value[0].'&nbsp;added unsucessfully'."<br />";
}
}
}
}