Skip to main content

Import Customers by csv file in Magento

Import Customers by csv file in Magento
here i developed script to import customers in magento by csv file.
csv file have following columns
########customerid (unique key) accesskey (customer type) emailaddress password firstname lastname companyname billingaddress1 city state postalcode country phonenumber faxnumber
###########

here i didn't use any magento's core file.

code is given below,i hope it will help you :)
set_time_limit(36000);
error_reporting(0);
/**
* @Use Script to import customer's data in Magento Database
* @createDate Mar 2010
* @author Manoj Ninave (manojninave@gmail.com)
**/

define("DB_HOST","localhost",TRUE); // database server name
define("DB_USER","root",TRUE); // database user
define("DB_PASS","",TRUE); // database user's password
define("DB_NAME","dbname",TRUE); // database name
require_once('db.class.php');

if( !isset($db_obj) ) {
$db_obj = new db;
}

$db_conect = $db_obj->connect();
//function to take country code from magento
//if the countr code are already in your csv file then //don't use this function.
function getCountryCode($countryName)
{
$xml = simplexml_load_file("territories.xml");
foreach($xml as $a => $b)
{
if($b == $countryName)
{
foreach($b->attributes() as $c)
{
return $c ;
}
}
}
}
echo "*******************Customer Import Module********************";
$file_handle = fopen("customers.csv", "r");
$i=0;

while (!feof($file_handle) ) {

$line_of_text = fgetcsv($file_handle, 1024);

if($i > 0)
{
$customerid =addslashes($line_of_text[0]);
$accesskey =addslashes($line_of_text[1]);
$emailaddress =addslashes($line_of_text[2]);
$password =md5($line_of_text[3]);
$firstname =addslashes($line_of_text[4]);
$lastname =addslashes($line_of_text[5]);
$companyname =addslashes($line_of_text[6]);
$billingaddress1 =addslashes($line_of_text[7]);
$billingaddress2 =addslashes($line_of_text[8]);
$city =addslashes($line_of_text[9]);
$state =addslashes($line_of_text[10]);
$postalcode =addslashes($line_of_text[11]);
$country =addslashes($line_of_text[12]);
$phonenumber =addslashes($line_of_text[13]);
$faxnumber =addslashes($line_of_text[14]);
$paysstatetax =addslashes($line_of_text[15]);
$taxid =addslashes($line_of_text[16]);
$emailsubscriber =addslashes($line_of_text[17]);
$percentdiscount =addslashes($line_of_text[18]);
$websiteaddress =addslashes($line_of_text[19]);
$discountlevel =addslashes($line_of_text[20]);
$customer_isanonymous =addslashes($line_of_text[21]);
$issuperadmin =addslashes($line_of_text[22]);
$allow_access_to_private_sections =addslashes($line_of_text[23]);
$customer_notes =addslashes($line_of_text[24]);
$Today = date("Y-m-d H:i:s");
$country_code = getCountryCode($country);

$sql_email = $db_obj->query("SELECT email from customer_entity where email='".$emailaddress."'",$db_conect);
$num_email = $db_obj->num_rows($sql_email);

if(!$emailaddress=="" && $num_email<=0) { $db_obj->query("INSERT INTO customer_entity(entity_type_id,attribute_set_id,website_id,email,group_id,increment_id,store_id,created_at,updated_at,is_active) VALUES (1,0,1,'".$emailaddress."',1,'',1,'".$Today."','".$Today."',1)",$db_conect);
//$increament_id++;

$sql_parent_id = $db_obj->query("SELECT max(entity_id) as id from customer_entity",$db_conect);

$rs_parent_id = $db_obj->fetch_array($sql_parent_id);
$parentId = $rs_parent_id['id'];

$db_obj->query("UPDATE customer_entity SET increment_id='".$parentId."' WHERE entity_id='".$parentId."'",$db_conect);

$db_obj->query("INSERT INTO customer_address_entity(entity_type_id,attribute_set_id,parent_id,created_at,updated_at,is_active) VALUES (2,0,'".$parentId."','".$Today."','".$Today."',1)",$db_conect);

$sql_max_entity_id = $db_obj->query("SELECT max(entity_id) as id from customer_address_entity",$db_conect);

$rs_max_entity_id = $db_obj->fetch_array($sql_max_entity_id);
$maxentityId = $rs_max_entity_id['id'];

$db_obj->query("INSERT INTO customer_address_entity_text(entity_type_id,attribute_id,entity_id,value) VALUES (2,23,'".$maxentityId."','".$billingaddress1."')",$db_conect);

$db_obj->query("INSERT INTO customer_address_entity_varchar(entity_type_id,attribute_id,entity_id,value) VALUES
(2,18,'".$maxentityId."','".$firstname."'),
(2,20,'".$maxentityId."','".$lastname."'),
(2,22,'".$maxentityId."','".$companyname."'),
(2,29,'".$maxentityId."','".$phonenumber."'),
(2,30,'".$maxentityId."','".$faxnumber."'),
(2,24,'".$maxentityId."','".$city."'),
(2,26,'".$maxentityId."','".$state."'),
(2,28,'".$maxentityId."','".$postalcode."'),
(2,25,'".$maxentityId."','".$country_code."')",$db_conect);

$db_obj->query("INSERT INTO customer_entity_int(entity_type_id,attribute_id,entity_id,value) VALUES
(1,13,'".$parentId."','".$maxentityId."'),
(1,14,'".$parentId."','".$maxentityId."')",$db_conect);

$db_obj->query("INSERT INTO customer_entity_varchar(entity_type_id,attribute_id,entity_id,value) VALUES
(1,5,'".$parentId."','".$firstname."'),
(1,7,'".$parentId."','".$lastname."'),
(1,3,'".$parentId."','Default Store View'),
(1,12,'".$parentId."','".$password."')",$db_conect);
}
}
$i++;
}

fclose($file_handle);
echo "Customer Imported Successfully.";

Comments

  1. It is very helpful to me.
    i wanted just like that.thank you Manoj.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. copy the following code in db.class.php file
    it will you for database connection and mysql functions



    Databse conneection file
    Author: Manoj Ninave
    manojninave@gmail.com
    created date:12/12/09


    class db {


    function connect() {
    $die = false;
    $link = @mysql_connect(DB_HOST, DB_USER,DB_PASS) or

    ($die = true);

    if($die){

    echo 'Database connection error!!!';

    echo 'A connection to the Database could not be

    established.
    ';
    echo 'Please check your username, password,

    database name and host.
    ';
    echo 'Also make sure mysql.class.php is

    rightly configured!

    ';

    exit();
    }

    mysql_select_db(DB_NAME);

    return $link;
    }

    // Returns an array that corresponds to the fetched row
    function fetch_array($query) {
    $query = mysql_fetch_array($query,MYSQL_ASSOC);
    return $query;
    }
    // To Execute Sql Query
    function query($sql,$res) {
    $query = mysql_query($sql) or die(mysql_error());
    return $query;
    }
    // Retrieves the number of rows from a result set
    function num_rows($query) {
    $query = mysql_num_rows($query);
    return $query;
    }
    // Get the number of affected rows by the last INSERT, UPDATE or DELETE query
    function affected_rows($query) {
    $query = mysql_affected_rows();
    return $query;
    }

    }

    ReplyDelete

Post a Comment