Hello, welcome to your in our blog. Hope you are doing great. If you have a Magento eCommerce store with large number of customer data like 200000 above and you are trying to export from Magento Admin Panel but Dataflow Profile failed to export data due to memory limit or something else. This article will help you to export customer data. Follow below instruction with code and execute the file you will get your customer data.
First of all, you need to create a file in you Magento root folder i.e: export-customer.php and follow below steps.
Include Mage.php from app folder that includes all Magento necessary class.
// Include Mage.php
require_once('app/Mage.php');
umask(0);
Mage::app();
Set memory limit for increase process execute memory. You can change it by your needs.
//Set memory limit
ini_set("memory_limit","1024M");
Add/Edit your desired fields you want to export by bellow code:
$_customersData[] = array(
'ID',
'Name',
'Email',
'Group',
'Telephone',
'Customer Since'
);
Now we will get customer data by below with page number and per page products limit by below code:
$page_num = 1; // Page Number i.e 1 = first page
$per_page_items = 10000; // Number of customer data in each page i.e: 10000
// Get customer data collection
$customers = Mage::getModel('customer/customer')->getCollection();
$customers->addAttributeToSelect('*');
$customers->setPage($page_num,$per_page_items);
$customers->setOrder('entity_id', 'DESC'); // Order by customer id as descending
After get customer data collection you need to format data as per your needs. See below that I did. I wrote comment every section of process.
// 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();
// Customer name
$customerName = $customer->getData('firstname')." ".$customer->getData('lastname');
$_customerTelephone = "";
// Customer Telephone Number from Billing Address
if($customer->getDefaultBilling()) {
$billingAddress = Mage::getModel('customer/address')->load($customer->getDefaultBilling());
if($billingAddress->getId()) {
$_customerTelephone = $billingAddress->getData('telephone');
}
}
// Customer Telephone Number from Shipping Address
if($customer->getDefaultShipping() && empty($_customerTelephone)) {
$shippingAddress = Mage::getModel('customer/address')->load($customer->getDefaultShipping());
if($shippingAddress->getId()) {
$_customerTelephone = $shippingAddress->getData('telephone');
}
}
// Strip whitespace from Telephone number
$_customerTelephone = trim($_customerTelephone);
$_customersData[] = array(
$customer->getData('entity_id'),
$customerName,
$customer->getData('email'),
$customerGroupName,
$_customerTelephone,
$customer->getData('created_at')
);
}
Now, you will get current customer data in $_customersData PHP variables and you are ready to export by save in a file as CSV or XLS. I saved as CSV by using Magento Varien_File_Csv built in class. Code below:
// Magento builtin class that will save your data as CSV
$csv = new Varien_File_Csv();
$csv->saveData($customerdata_path, $_customersData);
// Just a notification for you
echo "Customer data export from page ".$page_num." per page item ".$per_page_items;
You have all done. Save your codes and upload your Magento ROOT folder. Now you are ready to execute the file.
Finally, browse your fine by your browser i.e: http://www.example.com/export-customer.php. After execute first then change $page_num variable value and re-execute. Continue this process until complete your customer limit.
That’s all. Best of luck guys. Cheers!