Magento :: Export all customers and emails from orders

Magento export customer emails from ordersMagento export

Every now and then I am asked by different clients if I can export specific customer data, most often people need emails of their customers to use for newsletters or other types of marketing campaigns. There are two different ways to approach the problem either get all orders and get the customer email or any other related details that might be needed or you can use the customer collection which will give you all unique records for customers.

Setup magento API access

Before we dive in there is one thing that we have to do and that is we need to connect to magento API and setup a Mage APP. To do that we need first to include the Mage.php file which depends on the directory you choose to add your export file. In my case I’ll use the root Magento installation directory

/home/magento/export.php

Here is how to initialise the app:

error_reporting(E_ALL); // enable all errors
require_once ("./app/Mage.php");
umask(0);
Mage::app("default"); // load the app

Now all is set up and we can proceed with fetching the relevant data.

Export customer emails from orders

Magento provides easy access to customers and order through Collections:

A collection is a Model type containing other Models, it is basically used in Magento to handle product lists (ie. from a category or a bundle option), but not only.

Getting order is shown below:

$orders = Mage::getModel('sales/order')->getCollection()
->addFieldToFilter('status', 'complete') // get only completed orders
->addAttributeToSelect('customer_email') // add only specific attributes as we don't need all data
->addAttributeToSelect('customer_firstname')
->addAttributeToSelect('customer_lastname');

Once we have all orders, bear in mind that if you have thousands of orders the script could take quite long time to export all data, we can now get customers data and store it in an array.

$customersData = array();
foreach($orders as $order) {
    $customersData[] = array(
        'email' => $order->getCustomerEmail(),
        'name' => $order->getCustomerName() 
    );
}

That was easy, right? This approach is good especially in cases where you need to export some order specific data like shipping address or order items or even order ID, though bear in mind you might end up with duplicate emails as one customer could have multiple orders.

Export emails from customer collection

This approach should be slightly quicker form the former as we export only clients without order information thus we should have only unique emails.

$customerCollection = Mage::getModel('customer/customer')->getCollection();
$customerCollection->addAttributeToSelect(array(
'firstname', 'lastname', 'email'
));
$customersData = array();
foreach ($customerCollection as $customer) {
$customersData[] = array(
'name' => $customer->getFirstame() . ' ' . $customer->getLastname() ,
'email' => $customer->getEmail()
);
}

Once we have all data we can do different manipulations for example add it to CSV file and save to server or directly send for download through the browser.

Export customers’ emails with SQL query

If you have many order or customers the above examples may not work due to timeout. Though you still can use a old plain SQL and get the data directly from the database.

SELECT DISTINCT(o.customer_email)
FROM sales_flat_order o

And here is how get the data form the customer entity table:

SELECT ce.email
FROM customer_entity AS ce
It's only fair to share...Buffer this pageShare on Facebook0Tweet about this on TwitterShare on Google+0Share on LinkedIn0Share on Reddit0Pin on Pinterest0Email this to someone
About

Just a guy with strong interest in PHP and Web technologies

Posted in CMS, Magento, PHP Tagged with: , , , ,

Leave a Reply

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

*