Avenir | Drupal Development | Joomla Development | Magento Development | OsCommerce Development

Tuesday 27 March 2012

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


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

<?php

// 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';
}
}

?>

3 comments:

  1. Thanks so much for this post. This is awesome post I ever seen on internet. This is rare to find that’s why difficult to understand. Anyway, you are definitely someone that has
    something to say that people need to hear. Keep up the good work. Keep on inspiring the people.
    software development company

    ReplyDelete
  2. too bad its copied from http://mysillypointofview.richardferaro.com/

    ReplyDelete
  3. Innverse.com provides high impact IT solutions to its onshore-offshore clients in a cost effective manner. We have a team of skilled professionals proficient in providing cutting-edge solutions that will help you to leap ahead of your competitors
    Innverse Technologies is leading web development company in India offer web development solutions. Our work has earned us clients from across the globe and we make sure you get the value for your money and become part of our growing clients list.
    "

    ReplyDelete