Tuesday 25 November 2014

Magento Customer Import - Convert plain text password to magento format.

One of my recent task is to import about 10k customers into Magento database.

First thing came into my mind is to export the existing customer from magento and use the csv file to fill in the new customer values and import them back.
But after exporting the few existing customers, i came to know that the CSV header fields which Magento exported is not suitable for the import and missing fields for password, customer address etc.

After few searches, i found this SAMPLE CSV file. click to download

Filled in the CSV file and for the password_hash field, I had to develop a customer script to convert plain text password to Magento format password.

My custom script is below. It reads the CSV file which contains plain text password, Encrypts it and writes to a new CSV file. After you CSV file is ready go to the admin panel. In the menu select System -> Import/Export -> Dataflow- Profiles. You will see "import customer". Click on it and upload your CSV file and click "save & continue edit" button. Then Choose the imported file from the dropdown and click Run.

All the customers will be imported with their previous password and addresses.

<?php
$row = 1;
$fp = fopen('var/log/ss1-pass.csv', 'w');
$csvHeader = array('Text','Encrypted');
fputcsv( $fp, $csvHeader,",");

//generate random string for salt
function generateRandomString($length = 2) {
    $characters = '123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, strlen($characters) - 1)];
    }
    return $randomString;
}

if (($handle = fopen("ss1.csv", "r")) !== FALSE) {

    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
      $row++;
      $plainText =  trim($data[0]);
      $salt = generateRandomString(2);
      $encrypted = md5($salt.$plainText).":".$salt;
      fputcsv($fp, array($plainText,$encrypted), ",");
      //if($row==20){break;}
    }
   
 fclose($handle);
 fclose($fp);
}
?>


No comments:

Post a Comment