How to create a new Magento customer account from an external site?

In: Feeling guru|Techie Daw

22 Feb 2011

I’ve been receiving a lot of inquiries on how to create a new customer record on the fly using Mage.php or using the plugin Mage Enabler for WordPress. So, to save you time and also to provide a thread for this specific topic, here’s how to accomplish it.

First, include the file Mage.php from your Magento installation to your PHP file. Doing this will enable you to connect to your shop using the Mage object.

If you’re using Mage Enabler for WordPress, you may skip this part:

<?php
require_once ("/your/magento/app/Mage.php");
umask(0);

and here’s the rest of the code…

// Customer Information
$firstname = "John";
$lastname = "Smith";
$email = "johnsmith@localhost.local";
$password = "myverysecretpassword";

// Website and Store details
$websiteId = Mage::app()->getWebsite()->getId();
$store = Mage::app()->getStore();

$customer = Mage::getModel("customer/customer");
$customer->website_id = $websiteId;
$customer->setStore($store);

try {
	// If new, save customer information
	$customer->firstname = $firstname;
	$customer->lastname = $lastname;
	$customer->email = $email;
	$customer->password_hash = md5($password);
	if($customer->save()){
		echo $customer->firstname." ".$customer->lastname." information is saved!";
	}else{
		echo "An error occured while saving customer";
	}
}catch(Exception $e){
	// If customer already exists, initiate login
	if(preg_match('/Customer email already exists/', $e)){
		$customer->loadByEmail($email);

		$session = Mage::getSingleton('customer/session');
		$session->login($email, $password);

		echo $session->isLoggedIn() ? $session->getCustomer()->getName().' is online!' : 'not logged in';
	}
}

The above code tries to create a Magento customer account using the given parameters such as $firstname, $lastname, $email and $password. If the email doesn’t have a match in the database, the request proceeds. But if a match is found, it logs in the customer using the parameters $email and $password then checks if the customer is online or not logged in. There are actually more parameters that you can pass such as if you want to send confirmation message or not but these are the basic fields to help you get started.

I hope this will help you integrate the Magento registration to your external site ๐Ÿ™‚

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.

31 Responses to How to create a new Magento customer account from an external site?

Avatar

lorenzo UNITED KINGDOM

February 28th, 2011 at 1:43 am

Richard, this is a very useful example!

i’m pretty sure that others will also be interested in finding out your opinion about how would you go about synching two systems?

in my case not all WP users will become ‘customers’, but all magento customers have a WP user. i’m wondering what pitfalls i should be considering?

thanks

Avatar

Richard Feraro UNITED STATES

February 28th, 2011 at 4:45 pm

Hi Lorenzo,

What are the conditions to be considered to know which subscriber account will become a customer and vice-versa? Who gets to decide on that?

Avatar

Event Lover PHILIPPINES

February 28th, 2011 at 5:41 pm

ok po talaga yung mga tutorial niyo, detailed and easy to understand.

Avatar

Richard Feraro

March 1st, 2011 at 6:14 am

Thank you ๐Ÿ™‚

Avatar

Brian DENMARK

March 11th, 2011 at 7:52 am

Hi Richard..
Are you available for hire to do some freelance work… ?
If so, you are most welcome to contact me through my email.. I could use some help connecting WordPress and Magento to do some membership stuff..

Hope to hear from you..

Best regards
Brian Brandt
http://wpdk.dk (danish WordPress designer :-))

Avatar

Magento Links | Chris Reprogrammed

April 28th, 2011 at 9:29 pm

[…] How to create a new Magento customer account from an external site FishPig’s Magento Tutorials Yireo’s Magento Tutorials Branko Ajzele’s Magento Tutorials Alan Storm’s Magento Tutorials Fontis Blog […]

Avatar

Rait Lotamรตis ESTONIA

May 6th, 2011 at 4:23 am

Hey again, Richard.

I am finding it difficult to get the above code implemented into my WP install. The issue is, that the Enabler plugin does not work on admin pages for the time being. So I can’t put this code in the wp-login.php or user.php

Is the only solution to create a custom login page and pass on the credentials to WP from there?

Best regards,
Rait Lotamรตis

Avatar

Richard Feraro

May 6th, 2011 at 11:45 am

Hello Rait, try exploring the pluggable functions ๐Ÿ™‚

http://codex.wordpress.org/Pluggable_Functions

Avatar

rubab

June 19th, 2011 at 3:08 am

Can I insert Customer id through this method?

I am using following code to enter customers but I can’t figure out how to why Address, Postalcode, Country , state is not entering.
Right now Password is not require to generate for customers from my client.

Please check following code….

$newCustomer->setId(001)
->setFirstname(“Test008”)
->setLastname(“Test008”)
->setEmail(“email.2@email.com”)
->setGroupId(2)
->setAddress(“Test”)
->setCompany(“Home”)
->setCountryid(“CA”)
->setCity(“SWIFT CURRENT”)
->setPostcode(“V135645”)
->save();

and please tell me how to enter Customer Since date I used following in above code but not working. The above code is inserting wrong date due to that I can’t go on detail page because of error.

->setCustomer_Since(date(“M j, Y h:i:s A”))
->setCustomerSince(date(“M j, Y h:i:s A”))
->setCustomersince(date(“M j, Y h:i:s A”))

please reply me soon

Avatar

Richard Feraro

June 30th, 2011 at 8:33 pm

I usually use setData to do it. Also, is it a new record? If yes, then you must set the id as null since its autogenerated in the db.

Avatar

hotmonitor BULGARIA

July 24th, 2011 at 3:20 pm

Hello
Can I publish Magento Block in wordpress with this plugin?

Avatar

Richard Feraro PHILIPPINES

July 25th, 2011 at 6:05 pm

Yes, I already have a post about it.

Avatar

Elvis CROATIA

November 26th, 2011 at 5:54 am

Thanks for the php code – it works. However my Magento setup need email confirmations before customer become active. Emails after this way of creating a customer account are not sending so customer does not have a chance to confirm the email.

Is it possible to add ‘confirmed and active’ status immediattly and then send welcome email? Also newsletter check box?

Thanks

Avatar

Richard Feraro

November 26th, 2011 at 9:27 am

Hello Elvis,

Have you tried Mage::getModel('core/email_template')?
http://docs.magentocommerce.com/Mage_Core/Mage_Core_Model_Email_Template.html

Avatar

bruce lorio UNITED STATES

December 21st, 2011 at 3:57 am

richard we have a couple of project pending i was wondering if you are available for some free lance work.
you seem to be an expert in magento .
let me know please

Avatar

tuba INDIA

March 17th, 2012 at 4:30 am

wow, wonderful man!

Saved my enough time to develop a third party website which needs magento login credentials!

Thank you very much!

Avatar

Sam UNITED STATES

March 17th, 2012 at 4:36 am

yes, it worked perfectly!

Avatar

Nagui

April 14th, 2012 at 10:12 pm

Hi thinks for this info
i have a question about adresse and facturation adresse and if i want to add a new atribute to the inscription like a image for clients …..

thinks ๐Ÿ™‚ sory for my bad english ๐Ÿ˜€

Avatar

Vyom INDIA

April 25th, 2012 at 2:14 pm

Hi,

Is it possible to redirect the login to a store page. I have been able to create the login but when I try to redirect the url to a store page, the session does not show logged in.

Avatar

Larry Nix INDIA

June 11th, 2012 at 6:38 pm

Is it possible to refocus the sign in to a shop web page. I have been able to make the sign in but when I try to refocus the url to a shop web page, the period does not display signed in.

Avatar

mekix

November 26th, 2012 at 9:25 am

muchas gracias!

Avatar

Marek SLOVAKIA

December 16th, 2012 at 4:48 am

im not sure about custom fields created in magento for registration form. is there possibility solve it? im using some in reg. form, but in wp are not visible, only default fields

Avatar

EBIA AUSTRIA

April 2nd, 2013 at 9:15 pm

Hi Richard, thank you very much for this information, for which i was looking. I’ve lost too much time with wannabe-experts from StackOverflow. I would have just to see your blog.

ebia.eu/go -> HTML5 Development

Avatar

bojjaiah INDIA

August 5th, 2013 at 7:03 pm

Hi,

thank you very much for this information, Could you give a example for update and delete functions?

thanks in advance……..

Avatar

Harmony SWITZERLAND

February 21st, 2014 at 7:24 pm

Thank you for your post, it was really helpful !

I encountered a problem with Magento 1.7 however, after searching for a while I figured out that the preg_match function was helding a problem.
For my version of Magento (1.7.0.2) it isn’t preg_match(‘/Customer email already exists/’, $e) but preg_match(‘/This customer email already exists/’, $e) .

Avatar

ank

March 22nd, 2014 at 11:12 pm

Hi ,
for me the script gets to work as it register the cusotmer or logs him..howver when i go to the magento homepage the cusomter appears to be not logged in…

What cud be the reason…as the session was created externally?

what extra does one have to do so the user created externally appears logged in?

Avatar

Richard Feraro PHILIPPINES

March 26th, 2014 at 3:35 pm

Check the domain of the cookie. The two apps should share the same domain.

Avatar

Bradley Clampitt UNITED STATES

August 8th, 2014 at 2:57 am

Hey Richard,

This has come in handy for something I am trying to do, but the “Else” statements are not appearing, so if there is an error it just shows a blank page. I was testing it or am testing it and I changed the value of the password to see what would happen and I cannot get it to error out or echo out a statement.

Otherwise kudos for creating this for us! I am trying to combine an external registration that I can use for Magento, WordPress and other scripts, etc.

Best Regards,
Bradley

Avatar

Richard Feraro

August 15th, 2014 at 11:52 am

Were you able to solve this Bradley?

Avatar

parthi AUSTRALIA

March 26th, 2015 at 6:17 pm

nice work

Avatar

Himanshu

July 14th, 2015 at 5:49 pm

Hi Richard,

I am trying to find out how to fetch online customer data in real time Magento to other site.

Thanx

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 24 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.