Magento Import Tier Price

Below is the code to Magento Import Tier Price from csv

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//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");
}
1
2
3
4
5
6
7
8
9
<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>

No comments:

Post a Comment