Below is the code to Magento Import Tier Price from csv
//Magento Import Tier Price from csv require_once 'app/Mage.php'; umask(0); Mage::app('default'); if(isset($_FILES["file"]) && !empty($_FILES["file"])) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "
"; } else { echo "Upload: " . $_FILES["file"]["name"] . "
"; echo "Type: " . $_FILES["file"]["type"] . "
"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "
"; if (file_exists("var/export/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "var/export/" . $_FILES["file"]["name"]); echo "Stored in: " . "var/export/" . $_FILES["file"]["name"]; } } $tableName = Mage::getSingleton('core/resource') ->getTableName('catalog_product_entity_tier_price'); function CSVImport($table, $fields, $csv_fieldname='csv') { $handle = fopen("var/export/".$_FILES["file"]["name"],'r'); if(!$handle) die('Cannot open uploaded file.'); $row_count = 0; $rows = array(); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $row_count++; $records[] = $data; foreach($data as $key=>$value) { $data[$key] = "'" . addslashes($value) . "'"; } $rows[] = implode(",",$data); } fclose($handle); if(count($records)) { foreach($records as $key=>$value) { $tierprice[$value[4]][]=array( 'website_id' => 0, 'price_qty' => $value[8], 'price' => $value[9] ); } foreach($tierprice as $key=>$value) { $productid = Mage::getModel('catalog/product') ->getIdBySku($key); if($key=='sku') { continue; } foreach($value as $key1=>$val1) { $query = "insert into $table set entity_id='".$productid."', all_groups=1, qty=$val1[price_qty], value=$val1[price]"; $write = Mage::getSingleton('core/resource')->getConnection('core_write'); $write->query($query); } } print 'Successfully imported '.(int)($row_count-1).' record(s)'; } else { print 'Cannot import data - no records found.'; } } CSVImport($tableName, array('entity_id','all_groups','customer_group_id','qty', 'tier_type', 'value' ,'website_id'), "csv_file"); }
Tier Price Import
No comments:
Post a Comment