Skip to content

Instantly share code, notes, and snippets.

@igor822
Created October 23, 2017 01:35
Show Gist options
  • Save igor822/1fa731dba4293491a417c56bb2c5498e to your computer and use it in GitHub Desktop.
Save igor822/1fa731dba4293491a417c56bb2c5498e to your computer and use it in GitHub Desktop.
Import Magento Customer
<?php
require __DIR__. '/../app/Mage.php';
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$count = 0;
$file = fopen('customers.csv', 'r');
$indices;
while (($line = fgetcsv($file)) !== FALSE) {
$count++;
$num = count($line);
if ($count <= 1) {
for ($c = 0; $c < $num; $c++) {
$indices[$line[$c]] = '';
}
continue;
}
$data = $indices;
if (!empty($line[0]) && !empty($line[1])) {
$count2 = 0;
foreach($data as $key => $val) {
$data[$key] = $line[$count2];
$count2++;
}
createCustomer($data);
sleep(0.5);
unset($data);
}
}
function getStore($code)
{
static $store = [];
if (null == $store[$code]) {
$store[$code] = Mage::getModel('core/store')->load($code, 'code');
}
return $store[$code];
}
function getWebsite($code)
{
static $website = [];
if (null == $website[$code]) {
$website = Mage::getModel('core/website')
->getCollection()
->addFieldToFilter('code', array('eq', $code))
->getFirstItem();
$website[$code] = $website->getId();
}
return $website[$code];
}
function getRegion($name)
{
static $region = [];
if (null == $region[$name]) {
$region = Mage::getModel('directory/region')
->getCollection()
->addFieldToFilter('name', array('eq', $name))
->getFirstItem();
$region[$name] = $region->getId();
}
return $region[$name];
}
function createCustomer($data)
{
echo "\n Starting. {$data['email']}";
$customer = new Mage_Customer_Model_Customer();
try {
echo sprintf("\n %s %s %s \n", $data['firstname'], $data['middlename'], $data['lastname']);
$customer = Mage::getModel("customer/customer");
$store = getStore($data['_store']);
$customer->setWebsiteId(getWebsite($data['_website']));
$customer->setStore($store);
$customer->setFirstname($data['firstname'])
->setMiddleName($data['middlename'])
->setLastname($data['lastname'])
->setEmail($data['email'])
->setPassword(md5("myReallySecurePassword"));
$customer->save();
$address = Mage::getModel("customer/address");
$address->setCustomerId($customer->getId())
->setFirstname($data['firstname'])
->setMiddleName($data['middlename'])
->setLastname($data['lastname'])
->setCountryId($data['_address_country_id'])
->setRegionId(getRegion($data['_address_region']))
->setPostcode($data['_address_postcode'])
->setCity($data['_address_city'])
->setTelephone($data['_address_telephone'])
->setFax($data['_address_telephone'])
->setCompany($data['_address_company'])
->setStreet($data['_address_street'])
->setIsDefaultBilling('1')
->setIsDefaultShipping('1')
->setSaveInAddressBook('1');
echo '.';
$address->save();
echo '.';
$customer->save();
} catch(Exception $e) {
$content = 'register error ' . $data['email'] . '->' . $e->getMessage() . "\n\n\n";
file_put_contents('./account-error.log', $content);
}
echo " \n Suceeded \n\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment