Programmatically Export Customer Data in Magento

Here I will show you how to export customer data in Magento programmatically. This will help you when you have a Magento eCommerce store with a large number of customer data like 100000 or above. Because when you are trying to export a large number of customer data from Magento admin panel chances are Dataflow profile might fail to export data due to memory limit or something else. To overcome this problem the below script will help you.

First, you need to create a PHP file (i.e: export_customer_data.php) in your Magento root folder and follow the steps below

Include Mage.php in the newly created file that has all Magento necessary class.

// Include Mage.php
require_once('app/Mage.php');
umask(0);
Mage::app();

Set memory limit to increase the amount of memory to execute the process. You can change it as you wish.

//Set memory limit
ini_set("memory_limit","1024M");

Add your desired fields that you want to export

$_customersData[] = array(
    'ID',
    'Firstname',
    'Lastname',
    'Email',
    'Group',
    'Billing Firstname',
    'Billing Lastname',
    'Company',
    'Street Address',
    'City',
    'Country',
    'Region',
    'Region Id',
    'Telephone'
);

Now, get the customer data with page number and limit per page by the code below

$page_num = 1;
$per_page_items = 1000;

// Get the customer data collection
$customers = Mage::getModel('customer/customer')->getCollection();
$customers->addAttributeToSelect('*');
$customers->setPage($page_num, $per_page_items);
$customers->setOrder('entity_id', 'ASC');

After getting customer data, format it as follows

// Customer data loop
foreach ($customers as $key => $customer) {
    // Load customer all data from Id
    $customer = Mage::getModel("customer/customer")->load($customer->getId());

    // Get customer group name
    $customerGroupId = $customer->getGroupId();
    $customerGroupName = Mage::getModel('customer/group')->load($customerGroupId)->getCustomerGroupCode();
 
    $billingFirstname = $billingLastname = $company = $streetAddress = $city = $country = $region = $regionId = $telephone = "";
    // Customer Telephone Number from Billing Address
    if($customer->getDefaultBilling()) {
        $billingAddress = Mage::getModel('customer/address')->load($customer->getDefaultBilling());
        if($billingAddress->getId()) {
            $billingFirstname = $billingAddress->getData('firstname');
            $billingLastname = $billingAddress->getData('lastname');
            $company = $billingAddress->getData('company');
            $streetAddress = $billingAddress->getData('street');
            $city = $billingAddress->getData('city');
            $country = $billingAddress->getData('country_id');
            $region = $billingAddress->getData('region');
            $regionId = $billingAddress->getData('region_id');
            $telephone = $billingAddress->getData('telephone');
        }   
    }

    $_customersData[] = array(
        $customer->getData('entity_id'),
        $customer->getData('firstname'),
        $customer->getData('lastname'),
        $customer->getData('email'),
        $customerGroupName,
        $billingFirstname,
        $billingLastname,
        $company,
        $streetAddress,
        $city,
        $country,
        $region,
        $regionId,
        $telephone
    );
}

Finally, use the Magento builtin function Varien_File_Csv to save the data as CSV

// Magento builtin class that will save the data as CSV
$csv = new Varien_File_Csv();
$path_to_save = Mage::getBaseDir(). DS . 'data.csv';
$csv->saveData($path_to_save, $_customersData);

Note: Before running this script, create a file data.csv in your Magento root directory.

Complete code to export customer data:

<?php
// Include Mage.php
require_once('app/Mage.php');
umask(0);
Mage::app();

//Set memory limit
ini_set("memory_limit","1024M");

$_customersData[] = array(
    'ID',
    'Firstname',
    'Lastname',
    'Email',
    'Group',
    'Billing Firstname',
    'Billing Lastname',
    'Company',
    'Street Address',
    'City',
    'Country',
    'Region',
    'Region Id',
    'Telephone'
);

$page_num = 1;
$per_page_items = 1000;

// Get the customer data collection
$customers = Mage::getModel('customer/customer')->getCollection();
$customers->addAttributeToSelect('*');
$customers->setPage($page_num, $per_page_items);
$customers->setOrder('entity_id', 'ASC');

// Customer data loop
foreach ($customers as $key => $customer) {
    // Load customer all data from Id
    $customer = Mage::getModel("customer/customer")->load($customer->getId());

    // Get customer group name
    $customerGroupId = $customer->getGroupId();
    $customerGroupName = Mage::getModel('customer/group')->load($customerGroupId)->getCustomerGroupCode();
 
    $billingFirstname = $billingLastname = $company = $streetAddress = $city = $country = $region = $regionId = $telephone = "";
    // Customer Telephone Number from Billing Address
    if($customer->getDefaultBilling()) {
        $billingAddress = Mage::getModel('customer/address')->load($customer->getDefaultBilling());
        if($billingAddress->getId()) {
            $billingFirstname = $billingAddress->getData('firstname');
            $billingLastname = $billingAddress->getData('lastname');
            $company = $billingAddress->getData('company');
            $streetAddress = $billingAddress->getData('street');
            $city = $billingAddress->getData('city');
            $country = $billingAddress->getData('country_id');
            $region = $billingAddress->getData('region');
            $regionId = $billingAddress->getData('region_id');
            $telephone = $billingAddress->getData('telephone');
        }
    }

    $_customersData[] = array(
        $customer->getData('entity_id'),
        $customer->getData('firstname'),
        $customer->getData('lastname'),
        $customer->getData('email'),
        $customerGroupName,
        $billingFirstname,
        $billingLastname,
        $company,
        $streetAddress,
        $city,
        $country,
        $region,
        $regionId,
        $telephone
    );
}

// Magento builtin class that will save the data as CSV
$csv = new Varien_File_Csv();
$path_to_save = Mage::getBaseDir('var'). DS . 'data.csv';
$csv->saveData($path_to_save, $_customersData);

This code has been tested in Magento 1.9.X.
Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *