Wednesday 14 March 2012

simple way to upload magento product in csv file via coding

<?php
ini_set("max_execution_time",0);
ini_set('memory_limit', '2048M');
include_once("app/Mage.php");
Mage::app();
umask(0);
echo "<table>\n";
$row = 0;
$handle = fopen("1.csv", "r");
if($handle!=0){

    $data = fgetcsv($handle, 1000, ",");
        for($i=0; $i < count($data); $i++) {
        $column[$i] = $data[$i];
        }
    $count = 0;


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

       

        //print_r($data);


//        echo "<pre>";
//print_r($data);
//exit;


    if(count($data) == 0){
            //maill
           
    }else{
           
            $newproduct = Mage::getModel('catalog/product');
            $productId = $newproduct -> getIdBySku($data[0]);       
            if($productId) {
                $newproduct -> load( $productId );
            }
        $newproduct->setTypeId('simple');
        $newproduct->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
        $newproduct->setStatus(1);
        $newproduct->setSku($data[0]);
        $newproduct->setShape($data[2]);
        $newproduct->setCarat($data[3]);
        $newproduct->setColor($data[4]);
        $newproduct->setCalarity($data[5]);
        $newproduct->setCut_grade($data[6]);
        $newproduct->setCertificate($data[9]);
        $newproduct->setDepth($data[10]);
        $newproduct->setTable($data[11]);
        $newproduct->setWeight($data[8]);
        $newproduct->setGirdle($data[12]);
        $newproduct->setCulet($data[13]);
        $newproduct->setPolish($data[14]);
        $newproduct->setSymmetry($data[15]);
        $newproduct->setFlour($data[16]);
        $newproduct->setMeasurement($data[17]);
        $newproduct->setCert($data[20]);
        $newproduct->setCountry($data[26]);
        $newproduct->setTaxClassId(0);
        $newproduct->setWebsiteIDs(array(1));
        $newproduct->setStoreIDs(array(1));
        $newproduct->setStockData(array(
            'is_in_stock' => 1,
            'qty' =>$data[21] ,
            'manage_stock' => 1
        ));
   
        $newproduct->setAttributeSetId(4);
        $newproduct->setName('Atlanta '.' (' .$data[3]. ''.'- CARAT '.') '.$data[2]. ' - shape'. '  diamonds');
        $newproduct->setCategoryIds(array(2,48)); // array of categories it will relate to
        $newproduct->setDescription('ATLANTA DIAMOND');
        $newproduct->setShortDescription('ATLANTA DIAMOND');
        $newproduct->setPrice($data[7]);

    try {
        if (is_array($errors = $newproduct->validate())) {
            $strErrors = array();
            foreach($errors as $code=>$error) {
                $strErrors[] = ($error === true)? Mage::helper('catalog')->__('Attribute "%s" is invalid.', $code) : $error;
            }
            $this->_fault('data_invalid', implode("\n", $strErrors));
        }

        $newproduct->save();
    } catch (Mage_Core_Exception $e) {
        $this->_fault('data_invalid', $e->getMessage());
    }
/*//re-index the index management via coding
$process = Mage::getModel('index/process')->load($i);
    $process->reindexAll();*/

}


}}
else{ echo "PLEASE INSERT CSV FILE"; }
fclose($handle);
echo "</tbody>\n</table>";

?>

1 comment: