it's my blog anyway
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
23 Responses to How to create a new Magento customer account from an external site?
lorenzo
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
Richard Feraro
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?
Event Lover
February 28th, 2011 at 5:41 pm
ok po talaga yung mga tutorial niyo, detailed and easy to understand.
Richard Feraro
March 1st, 2011 at 6:14 am
Thank you
Brian
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
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 [...]
Rait Lotamõis
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
Richard Feraro
May 6th, 2011 at 11:45 am
Hello Rait, try exploring the pluggable functions
http://codex.wordpress.org/Pluggable_Functions
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
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.
hotmonitor
July 24th, 2011 at 3:20 pm
Hello
Can I publish Magento Block in wordpress with this plugin?
Richard Feraro
July 25th, 2011 at 6:05 pm
Yes, I already have a post about it.
Elvis
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
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
bruce lorio
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
tuba
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!
Sam
March 17th, 2012 at 4:36 am
yes, it worked perfectly!
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
Vyom
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.
Larry Nix
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.
mekix
November 26th, 2012 at 9:25 am
muchas gracias!
Marek
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
EBIA
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