RSS

Create Customer in Magento Programmatically

08 Jul

CREATE MAGENTO CUSTOMER ACCOUNT PROGRAMMATICALLY

 Using Magento Class / API, we can create a customer account outside of Magento. It is useful when you want to register a certain customers that registering in a form that OUTSIDE of your Magento site. So, the concept here is that you will create a Magento customer account programmatically but ofcourse using Magento API. So here’s the code.
<code><?php
error_reporting(E_ALL | E_STRICT);
$mageFilename = 'app/Mage.php';

if (!file_exists($mageFilename)) {
    if (is_dir('downloader')) {
        header("Location: downloader");
    } else {
        echo $mageFilename." was not found";
    }
    exit;
}
require_once $mageFilename;
Varien_Profiler::enable();
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);

Mage::app('default');

$customer_email = 'test@testemail.com';  // email adress that will pass by the questionaire 
$customer_fname = 'test_firstname';      // we can set a tempory firstname here 
$customer_lname = 'test_lastname';       // we can set a tempory lastname here 
$passwordLength = 10;                    // the lenght of autogenerated password

$customer = Mage::getModel('customer/customer');
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
$customer->loadByEmail($customer_email);
/*
* Check if the email exist on the system.
* If YES,  it will not create a user account. 
*/

if(!$customer->getId()) {

   //setting data such as email, firstname, lastname, and password 

  $customer->setEmail($customer_email); 
  $customer->setFirstname($customer_fname);
  $customer->setLastname($customer_lname);
  $customer->setPassword($customer->generatePassword($passwordLength));

}
try{
  //the save the data and send the new account email.
  $customer->save();
  $customer->setConfirmation(null);
  $customer->save(); 
  $customer->sendNewAccountEmail();
}

catch(Exception $ex){

}
?></code>
Advertisements
 
2 Comments

Posted by on July 8, 2013 in Magento

 

Tags:

2 responses to “Create Customer in Magento Programmatically

  1. Kathy Daunt

    July 10, 2013 at 1:58 pm

    Hi Sudheer,

    I am trying to delete particular customer order in Magento.

    Using the below code:
    ……
    print_r($order->count()); To choose particular id order
    ……
    {
    echo $ord->getIncrementId() . ‘’;
    $ord->delete(); //This statement is to delete order
    } ?>

    But doesn’t workout for me. The order remains the same. It is not deleted

     
  2. Kathy Daunt

    July 10, 2013 at 2:00 pm

    Hi Sudheer,

    I am trying to delet a particular customer order

    by using below code:
    ….
    print_r($order->count());// to choose a particular

    id order
    ….
    {
    echo $ord->getIncrementId() . ‘’;
    $ord->delete(); //This statement is to delete

    order
    } ?>

    But nothing get deleted. can you help if you have

    any other sort of coding

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: