Avenir | Drupal Development | Joomla Development | Magento Development | OsCommerce Development
Showing posts with label external site. Show all posts
Showing posts with label external site. Show all posts

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

?>