Hope you are doing great. If you have a Magento eCommerce store with large number of customer data like 200000 or above and you are trying to export from Magento Admin Panel chances are Dataflow profile might fail to export data due to memory limit or something else. This article will help you to get customer data exported properly.
First off, you need to create a file in your Magento root folder i.e: export-customer.php and the follow steps below
Include Mage.php from app folder that has all Magento necessary class.
// Include Mage.php require_once('app/Mage.php'); umask(0); Mage::app();
Set memory limit to increase to amount of memory to execute the process. You can change it as your needs.
//Set memory limit ini_set("memory_limit","1024M");
Add/Edit your desired fields you want to export by the following code:
$_customersData[] = array( 'ID', 'Name', 'Email', 'Group', 'Telephone', 'Customer Since' );
Now we will get customer data with page number and product limit per page by the code below:
$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 getting customer data you will need to format it as your needs. See below what I did, comments are added in every section of the 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 it will be ready to export in a CSV or XLS file. 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 are all set, now save your codes and upload to the Magento ROOT folder.
Finally, browse the file by using a browser i.e: http://www.example.com/export-customer.php. After first execution change value of the $page_num variable and re-execute. Continue this process until completing your customer limit.
That’s all, thank you !
P.S. We would appreciate if you help spread the word about our magento extensions