How to pull the list of customers from Magento to an external site?

If you’re thinking of creating a page that will display your customer list from Magento to a different PHP-based application, you can use the script below to do so.

First, let’s create a file called index.php and inside it, create function (I named it getCustomers()) that will extract an array of customer list and their information. The function should be able to access the Mage.php file of your Magento instance.

<?php
function getCustomers() {
	/* Magento's Mage.php path
	 * Mage Enabler users may skip these lines
	 */
	require_once ("../magento/app/Mage.php");
	umask(0);
	Mage::app("default");
	/* Magento's Mage.php path */

	/* Get customer model, run a query */
	$collection = Mage::getModel('customer/customer')
				  ->getCollection()
				  ->addAttributeToSelect('*');

	$result = array();
	foreach ($collection as $customer) {
		$result[] = $customer->toArray();
	}

	return $result;
}
?>

Once you’re done with the function, add the HTML tags needed to create the a page with a table for the customer information.

<html>
<head>
<title>Customers</title>
<style>
table {
	border-collapse: collapse;
}
td {
	padding: 5px;
	border: 1px solid #000000;
}
</style>
</head>
<body>
<table>
<tr>
	<td>ID</td>
	<td>Lastname</td>
	<td>Firstname</td>
	<td>Email</td>
	<td>Is Active?</td>
	<td>Date Created</td>
	<td>Date Updated</td>
</tr>
</table>
</body>
</html>

After line 47, press return/enter key and add the following script to loop through the array result of our getCustomer() function:

<?php
$result = getcustomers();
if(count($result) > 0){
	foreach($result as $key => $value){
		echo "<tr>";
			echo "<td>".$value['entity_id']."</td>";
			echo "<td>".$value['lastname']."</td>";
			echo "<td>".$value['firstname']."</td>";
			echo "<td>".$value['email']."</td>";
			echo "<td>";
			echo $value['is_active'] == 1 ? "Yes" : "No";
			echo "</td>";
			echo "<td>".$value['created_at']."</td>";
			echo "<td>".$value['updated_at']."</td>";
		echo "</tr>";
	}
}else{
	echo "<tr><td colspan=\"7\">No records found</td></tr>";
}
?>

There are several available information within the array that you can also use. Here’s an example of a record result from our function:

[0] => Array
	(
		[entity_id] => 1
		[entity_type_id] => 1
		[attribute_set_id] => 0
		[website_id] => 1
		[email] => [email protected]
		[group_id] => 1
		[increment_id] => 000000001
		[store_id] => 1
		[created_at] => 2007-08-30 23:23:13
		[updated_at] => 2008-08-08 12:28:24
		[is_active] => 1
		[firstname] => John
		[lastname] => Doe
		[password_hash] => 2049484a4020ed15d0e4238db22977d5:eg
		[prefix] =>
		[middlename] =>
		[suffix] =>
		[taxvat] =>
		[default_billing] => 274
		[default_shipping] => 274
	)

Save the file as index.php and access it through your browser. It should display a table similar to the one below:

Sample table of customers from Magento

Sample table of customers from Magento

Here’s the index.php for your reference:

<?php
function getcustomers() {
	/* Magento's Mage.php path 
	 * Mage Enabler users may skip these lines
	 */
	require_once ("../magento/app/Mage.php");
	umask(0);
	Mage::app("default");
	/* Magento's Mage.php path */
	
	/* Get customer model, run a query */
	$collection = Mage::getModel('customer/customer')
				  ->getCollection()
				  ->addAttributeToSelect('*');
	
	$result = array();
	foreach ($collection as $customer) {
		$result[] = $customer->toArray();
	}
	
	return $result;
}
?>
<html>
<head>
<title>Customers</title>
<style>
table {
	border-collapse: collapse;
}
td {
	padding: 5px;
	border: 1px solid #000000;
}
</style>
</head>
<body>
<table>
<tr>
	<td>ID</td>
	<td>Lastname</td>
	<td>Firstname</td>
	<td>Email</td>
	<td>Is Active?</td>
	<td>Date Created</td>
	<td>Date Updated</td>
</tr>
<?php
$result = getcustomers();
if(count($result) > 0){
	foreach($result as $key => $value){
		echo "<tr>";
			echo "<td>".$value['entity_id']."</td>";
			echo "<td>".$value['lastname']."</td>";
			echo "<td>".$value['firstname']."</td>";
			echo "<td>".$value['email']."</td>";
			echo "<td>";
			echo $value['is_active'] == 1 ? "Yes" : "No";
			echo "</td>";
			echo "<td>".$value['created_at']."</td>";
			echo "<td>".$value['updated_at']."</td>";
		echo "</tr>";
	}
}else{
	echo "<tr><td colspan=\"7\">No records found</td></tr>";
}
?>
</table>
</body>
</html>

About the author

Richard Feraro is a Magento Enterprise Certified developer from Manila, Philippines with 14 years of solid open-source development experience using Linux, Apache, MySQL & PHP.

Leave a Reply

  1. Hi,

    How would you be able to use the Mage create customer form/block in the wordpress page?

    when we modify the reg form from the magento backend, we want it to be reflected in the customer reg form in the wp front end too.

    We want the customer to be created in the mage database, not the wp database

    Thanks

  2. Hi,

    Will you be able to advise me if it is possible to share customers list between 2 different magento installations, and if it is possible, is it possible if a customer login into any of the 2 magento site and he is automatically logged in into the other site?

    Thanks!!

    • Hello Darren,

      I haven’t tried it yet but as long as the cookie paths are the same, they should share the same session.

      Thanks 🙂

  3. Hi,

    I am very green when it comes to coding, [please if you can help me or point me to someone or software that can do this i will appreciate. I have a signup page on my site which emails me new user details then i create them on an external site. However I am looking to automate this by having a script which takes that info and logs in to the external site which i dont own but which i am a reseller, the script should login with my username and password and create the user and then bring up a success page on my site as well as email the customer a welcome message and send me a new user signed up to me.

    Thanks in advance

    Ramon

  4. well thanks for the great tutorial i want to find out the top 10 users results only show 10 customers. i have a database magentousers_points db where i store my customer id as user_id and points now from there i have to make the list right so

    how do i get a loop like – fetch 10 users order by points desc limit 10 and from their i am getting the points of the users.

    Now the results show id and points ….. and from that id there will be query to magento customer to get the name of the customer which you have already described i guess.

    so can you tell me how i will make it ?

  5. Awesome stuff, Thank you VERY much!

    Is there any way to add this list into a cms page or stack block?

    Thanks again!

  6. Thanks again!

    Keep getting a php error:

    Allowed memory size of 67108864 bytes exhausted (tried to allocate 87 bytes) in mypath/lib/Varien/Data/Collection.php on line 550

    any suggestions? Already tried increasing memory_limit in php.ini

    🙁

  7. Hi Richard,

    It’s in a Magento static block, and It happens even set at 512MB. My customer list is roughly 9,000.

    Thank you!

  8. Hi,

    Same pb “Allowed memory size of “….

    I’ve got more than 30000 customers.

    Perhaps the request needs filter ?

    Regards,

  9. exactly what i was looking for, if doing this on a rubbish server i would recommend adding:

    $collection->getSelect()->limit(200,0);

    before the foreach loop so you can pull them out in chunks.

    Pete

  10. Hi, Richard, it’s really help, question is how to import other side user to magento system, what’s necessary column in magento for add new user?

  11. i have two sites abc.com , xyz.com. Now i want the user to sign up in one site (abc.com) and the same credentials should work on xyz.com

    Both of them on magento and have their individual inventory and admin panel

    • Have you tried exploring the multi-site capabilities of Magento? If those two sites are in the same Magento instance, then you just have to make sure to set the configuration below to:

      System > Configuration > Default > Customer Configuration > Share Customer Accounts = Global

  12. Hi,

    Thanks for the code. Its perfect.

    Just have a query, is it possible to add a column to see total lifetime sales and last shopping date for each customer?

    Thanks Again for the code.