Magento :: Export all customers and emails from orders
Magento 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
Here is how to initialise 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:
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.
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.
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.
And here is how get the data form the customer entity table:
Leave a Reply