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

In: Feeling guru|Techie Daw

22 Feb 2011
Advertisement

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.

Advertisement

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 :)

Advertisement

About the author

Richard Feraro is a PHP Developer living in Manila, Philippines. He has been programming for nine (9) years and working on different open source application mostly done in PHP and MySQL.

15 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

Comment Form

About my blog

This is a programmer's blog. Whenever I encounter a difficult coding task, I make sure that I'll be able to share it here in the hope that others may find it useful. It's my way of giving back to the open source community who have been a great help to me as well.

Share This Post

or bookmark to your mobile

Bookmark: http://mysillypointofview.richardferaro.com/2011/02/22/create-new-magento-customer-from-external-site/

Advertisement

WordPress + Magento

Proudly Pinoy!

Proudly Pinoy!

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.

Viewers by Country

in India 3401 (21 %) 3934 (20 %)
us United States 2700 (17 %) 3507 (18 %)
gb United Kingdom 929 (6 %) 1182 (6 %)
de Germany 808 (5 %) 1011 (5 %)
nl Netherlands 571 (4 %) 710 (4 %)
fr France 516 (3 %) 655 (3 %)
ph Philippines 442 (3 %) 669 (3 %)
br Brazil 427 (3 %) 502 (3 %)
it Italy 343 (2 %) 438 (2 %)
es Spain 334 (2 %) 446 (2 %)
ca Canada 324 (2 %) 404 (2 %)
au Australia 321 (2 %) 378 (2 %)
vn Vietnam 289 (2 %) 369 (2 %)
pk Pakistan 279 (2 %) 305 (2 %)
cn China 256 (2 %) 345 (2 %)
ro Romania 241 (2 %) 292 (1 %)
ua Ukraine 234 (1 %) 298 (2 %)
id Indonesia 140 (1 %) 167 (1 %)
pl Poland 131 (1 %) 161 (1 %)
sg Singapore 126 (1 %) 171 (1 %)
Total Countries: 139
Total Visits: 15858
Total Pageviews: 19789