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

In: Feeling guru|Techie Daw

7 Sep 2010

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] => john.doe@example.com
		[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.

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

Avatar

magewp UNITED KINGDOM

January 13th, 2011 at 11:15 pm

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

Avatar

Richard Feraro UNITED STATES

February 22nd, 2011 at 12:05 am

Are you referring to this? Regarding the Magento blocks, I already posted an article on how to do that 🙂

Avatar

Darren SINGAPORE

April 14th, 2011 at 4:45 pm

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!!

Avatar

Richard Feraro UNITED STATES

April 14th, 2011 at 7:10 pm

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 🙂

Avatar

Ramon

May 25th, 2011 at 3:47 pm

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

Avatar

man_in_black

February 20th, 2012 at 6:06 pm

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 ?

Avatar

JP UNITED STATES

March 4th, 2012 at 2:41 am

Awesome stuff, Thank you VERY much!

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

Thanks again!

Avatar

Richard Feraro

March 4th, 2012 at 12:23 pm

Well, yeah. That’s the idea of it. I think someone did it already, check out the comments area.

Avatar

JP UNITED STATES

March 5th, 2012 at 7:48 am

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

🙁

Avatar

Richard Feraro PHILIPPINES

March 5th, 2012 at 9:09 am

What is the value you set in memory_limit? Does it happen in WordPress? If yes, check the link below:
http://codex.wordpress.org/Editing_wp-config.php#Increasing_memory_allocated_to_PHP

Avatar

JP UNITED STATES

March 5th, 2012 at 9:59 am

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!

Avatar

JP UNITED STATES

March 14th, 2012 at 10:40 pm

So how would you go about adding the company name to the table?

Avatar

surflibre

September 6th, 2012 at 3:30 pm

Hi,

Same pb “Allowed memory size of “….

I’ve got more than 30000 customers.

Perhaps the request needs filter ?

Regards,

Avatar

Richard Feraro PHILIPPINES

September 11th, 2012 at 12:14 am

How about adding limit per quest instead of pulling ALL customer records?

Avatar

Peter

November 15th, 2012 at 6:00 pm

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

Avatar

Richard Feraro

December 4th, 2012 at 3:19 am

Thank you for the tip 😉

Avatar

nathan AUSTRALIA

May 28th, 2013 at 10:39 pm

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?

Avatar

gary PHILIPPINES

July 20th, 2013 at 3:41 am

how about migration from joomla to magento can you teach me how chard

Avatar

satyanarayana

November 8th, 2013 at 9:30 pm

How to pull the individual customer information from Magento to an external site?

Avatar

Mahendra INDIA

April 4th, 2014 at 7:22 pm

Gr8 stuff…Saved my time. Thank u.

Avatar

Les NEW ZEALAND

July 24th, 2015 at 11:50 am

Most useful, thank you =)

Avatar

nikunj vaghasiya INDIA

August 4th, 2015 at 2:25 pm

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

Avatar

Richard Feraro PHILIPPINES

August 6th, 2015 at 12:36 am

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

Avatar

hiten

October 29th, 2015 at 2:14 pm

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.

Comment Form

About my blog

This blog is targeted towards all levels of developers and also designers. Various topics are discussed such as Magento, PHP & MySQL, jQuery, WordPress, CSS and HTML.

Booking.com

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 5,242 other subscribers

WordPress + Magento

Recent Trackbacks

Archives

Disclaimer

All entries in this blog are my opinion and don't necessarily reflect the opinion of my employer or my mom.