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 = "[email protected]";
$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.

By Richard Feraro

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 thoughts on “How to create a new Magento customer account from an external site?”
  1. 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

    1. 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?

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

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

  4. 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 protected]”)
    ->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

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

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

  6. wow, wonderful man!

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

    Thank you very much!

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

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

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

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

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

  12. Hi,

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

    thanks in advance……..

  13. 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) .

  14. 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?

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

  16. Hi Richard,

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

    Thanx

Leave a Reply