Friday, January 13

customer registration programmatically in Magento code from an external php file

In this post we’ll show you how to customer registration programmatically in magento using with exteranl php file. By using following code we can register user and create seeeion on user or customer.
  • This post is use for register user or customer registration programmatically in Magento.
  • first we create user ,if user is not exist then insert data into database then we will go for login to user programmatically.
  • add you detail in variable and enjoy the code.       
set_time_limit(0);
ini_set('memory_limit', '1024M');
include_once "app/Mage.php";  // Mage file include
include_once "downloader/Maged/Controller.php"; // Controller file include
error_reporting(E_ALL | E_STRICT);

$website_Id = Mage::app()->getWebsite()->getId();
$store_detail = Mage::app()->getStore();

$setFirstname = "Firstname"; // add  First name
$setLastname = "setLastname"; // add Last name
$setEmail = "Ttest123@gm.co"; // add  Email id
$setPassword = "password@789"; // add  password
/*$setPostcode = "989898"; // add  Post code
$setCity = "Sydney "; // add  city of user
$setRegion = "New South Wales";
$setTelephone = "99999999999";
$setFax = "123456";
$setCompany = "Australia";
$setStreet = "in Australia some place";*/
$customer = Mage::getModel("customer/customer");
$customer ->setWebsiteId($website_Id)
->setStore($store_detail)
->setFirstname($setFirstname)
->setLastname($setLastname)
->setEmail($setEmail)
->setPassword($setPassword);
try{
    $results = $customer->save();   
    $getEntityId = $results->getEntityId(); // get user id
}
catch (Exception $e) {
    // bug or user is exist
    echo $e->getMessage(); // print Exception Message
}

Mage::getSingleton('core/session', array('name' => 'frontend'));

$sessionCustomer = Mage::getSingleton("customer/session");
$red_url = "http://www.onlinecode.org/";
if($sessionCustomer->isLoggedIn()) {
    header('Location: '. $red_url);
    break;
}
else{
    //echo $setEmail." ".$setPassword;
    loginUser($setEmail,$setPassword);
    //header('Location: '. $red_url);
    //break;
}
// function for login user programmatically
function loginUser($email,$password){
    umask(0);
    ob_start();
    session_start();
    Mage::app('default');
    Mage::getSingleton("core/session", array("name" => "frontend"));
    $website_Id = Mage::app()->getWebsite()->getId();
    $store_detail = Mage::app()->getStore();
    $customer_detail = Mage::getModel("customer/customer");
    $customer_detail->website_id = $website_Id;
    $customer_detail->setStore($store_detail);
    try
    {
        $customer_detail->loadByEmail($email);
        $session = Mage::getSingleton('customer/session')->setCustomerAsLoggedIn($customer_detail);
        $session->login($email,$password);
    }
    catch(Exception $e)
    {
        echo $e->getMessage();// print Exception Message
    }
}

Customer / user registration programmatically in Magento

$website_Id = Mage::app()->getWebsite()->getId();
$store_detail = Mage::app()->getStore();

$setFirstname = "Firstname"; // add  First name
$setLastname = "setLastname"; // add Last name
$setEmail = "Ttest123@gm.co"; // add  Email id
$setPassword = "password@789"; // add  password
/*$setPostcode = "989898"; // add  Post code
$setCity = "Sydney "; // add  city of user
$setRegion = "New South Wales";
$setTelephone = "99999999999";
$setFax = "123456";
$setCompany = "Australia";
$setStreet = "in Australia some place";*/
$customer = Mage::getModel("customer/customer");
$customer ->setWebsiteId($website_Id)
->setStore($store_detail)
->setFirstname($setFirstname)
->setLastname($setLastname)
->setEmail($setEmail)
->setPassword($setPassword);
try{
    $results = $customer->save();   
    $getEntityId = $results->getEntityId(); // get user id
}
catch (Exception $e) {
    // bug or user is exist
    echo $e->getMessage(); // print Exception Message
}

Mage::getSingleton('core/session', array('name' => 'frontend'));

$sessionCustomer = Mage::getSingleton("customer/session");
$red_url = "http://www.onlinecode.org/";
if($sessionCustomer->isLoggedIn()) {
    header('Location: '. $red_url);
    break;
}
else{
    //echo $setEmail." ".$setPassword;
    loginUser($setEmail,$setPassword);
    //header('Location: '. $red_url);
    //break;
}
// function for login user programmatically
function loginUser($email,$password){
    umask(0);
    ob_start();
    session_start();
    Mage::app('default');
    Mage::getSingleton("core/session", array("name" => "frontend"));
    $website_Id = Mage::app()->getWebsite()->getId();
    $store_detail = Mage::app()->getStore();
    $customer_detail = Mage::getModel("customer/customer");
    $customer_detail->website_id = $website_Id;
    $customer_detail->setStore($store_detail);
    try
    {
        $customer_detail->loadByEmail($email);
        $session = Mage::getSingleton('customer/session')->setCustomerAsLoggedIn($customer_detail);
        $session->login($email,$password);
    }
    catch(Exception $e)
    {
        echo $e->getMessage();// print Exception Message
    }
}

No comments:

Post a Comment

10 Best Chatting Apps In India

  The modern world is all about Internet. There was a time when people use to pay high telephone bills to stay in touch with their friends a...