Tuesday, May 31, 2011

Magento Import Tier Price


<?php
//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"] . "<br />";
}
 else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

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");
}
?>
<html>
<head>
<title>Import Tier Prices</title>
</head>
<body>
<div style="margin-top:200px;margin-left:250px; border:1px solid #999999;width:300px" >
<div style="color:#996600;text-decoration:underline;margin-left:80px;font-weight:bold" >Tier Price Import </div>
<div style="margin-top:20px;padding-left:5px">
<form name="importform" method="post" action="" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" name="import" value="Import" />
</form>
</div>
</div>
</body>
</html>



2 comments:

  1. Would you please tell me the CSV format for tier price update script

    ReplyDelete
  2. I’m not sure, but did you had a look at this: http://www.mag-manager.com/useful-articles/tipstricks/how-to-import-magento-tier-prices

    ReplyDelete