Skip to content

Instantly share code, notes, and snippets.

@nhp
Last active July 30, 2020 11:57
Show Gist options
  • Select an option

  • Save nhp/58f520bf1817b522675f to your computer and use it in GitHub Desktop.

Select an option

Save nhp/58f520bf1817b522675f to your computer and use it in GitHub Desktop.
Magento customer migration from one instance to another even with version changes (max tested difference: mageCE 1.5 > 1.9)
<?php
set_time_limit(0);
$pathTo = '../../';
$magentoFilename = $pathTo . 'app/Mage.php';
require_once $magentoFilename;
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$resource = Mage::getSingleton('core/resource');
$read = $resource->getConnection('core_read');
$addressfields = getAttributes(2);
$fieldnames = getAttributes(1," AND attribute_code !='default_billing' AND attribute_code !='default_shipping'");
$fieldnames[] = 'current_server_entity_id';
$fieldnames[] = 'addressess';
$fieldsCount = count($fieldnames);
$fp = fopen('customers.csv', 'w');
fputcsv($fp, $fieldnames);
$customerCollection = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToSelect('*')
->addFieldToFilter('website_id',array('eq'=>'1'))
->addFieldToFilter('entity_id',array('gt'=>'1')); // entity id from
foreach ($customerCollection as $customer) {
$data = array();
$addressData = array();
foreach ($customer->getAddresses() as $address)
{
foreach ($addressfields as $field)
{
$data[$field] = $address->getData($field);
}
$data['default_billing'] = ($customer->getDefaultBilling() == $address->getId());
$data['default_shipping'] = ($customer->getDefaultShipping() == $address->getId());
$addressData[] = $data;
}
$data = array();
foreach ($fieldnames as $field)
{
$data[] = $customer->getData($field);
}
$data[$fieldsCount-2] = $customer->getId();
//address data will be stored in the CSV which we can unserialize while exporting
if (count($addressData) >0)
{
$data[$fieldsCount-1] = serialize($addressData);
}
fputcsv($fp, $data);
echo '<br>CustomerId: ' . $customer->getId();
}
fclose($fp);
/**
* Get Attributes
* @param integer $typeId
* @param string $extraCondtion
* @return array $fieldnames
*/
function getAttributes($typeId = 1,$extraCondtion='')
{
$resource = Mage::getSingleton('core/resource');
$read = $resource->getConnection('core_read');
/* eav attributes */
$query = 'SELECT attribute_code FROM eav_attribute
WHERE entity_type_id = ' . $typeId;
$query .= $extraCondtion;
$attributes = $read->fetchAll($query);
$fieldnames = array();
foreach ($attributes as $attribute)
{
$fieldnames[] = $attribute['attribute_code'];
}
return $fieldnames;
}
<?php
$magentoFilename = '../../app/Mage.php';
require_once $magentoFilename;
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$customers = get_csv_to_array('customers.csv');
foreach ($customers as $customerInfo)
{
$addresses = array_pop($customerInfo);
$nzCustomerId = array_pop($customerInfo);
$customer = Mage::getModel("customer/customer");
/// IMPORTANT : this localhost server only having store id 1 & website 1 SHOULD REMOVE THESE LINES or assign the correct store_id and website Id here
$customerInfo['store_id'] = 1;
$customerInfo['website_id'] = 1;
$customer->setData($customerInfo);
try
{
$customer->save();
$customerId = $customer->getId();
}
catch (Exception $error)
{
Mage::log($error->getMessage(). ' : ' . $nzCustomerId, null, 'customernotsaved.log');
continue;
}
if($addresses)
{
$addressData = unserialize($addresses);
foreach ($addressData as $address)
{
$is_default_billing = array_pop($address);
$is_default_shipping = array_pop($address);
$customAddress = Mage::getModel('customer/address');
$customAddress->setData($address)
->setCustomerId($customerId);
if($is_default_billing ==1)
{
$customAddress->setIsDefaultBilling(1)
->setSaveInAddressBook('1');
}
if($is_default_shipping ==1)
{
$customAddress->setIsDefaultShipping(1)
->setSaveInAddressBook('1');
}
try
{
$customAddress->save();
}
catch (Exception $error)
{
Mage::log($error->getMessage(). ' : ' . $nzCustomerId, null, 'customeraddressnotsaved.log');
}
}
}
Mage::log($nzCustomerId . ' => '. $customerId , null, 'customersaved.log');
}
function get_csv_to_array($file)
{
$row = 0;
$coloumnNames = array();
$items = array();
if (($handle = fopen($file, "r")) !== FALSE)
{
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$num = count($data);
for ($c=0; $c < $num; $c++)
{
if($row ==0)
{
$coloumnNames[] = $data[$c];
}
else
{
$rowData[$coloumnNames[$c]] = $data[$c];
}
}
if($row >0)
{
$items[] = $rowData;
}
$row++;
}
fclose($handle);
}
return $items;
}
@nhp
Copy link
Copy Markdown
Author

nhp commented Nov 12, 2014

This solution works pretty well to migrate customers from one installation to another (in my case 1.5 > 1.9).
With the standard import/export feature it is not possible to export many customers.

Solution was not created by me but was found here: http://anilpaul.com/magento-export-and-import-customer-from-one-server-to-another/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment