Programmatically adding new customers to the Magento store

/
/
181 Views
Hello Developers,
When I was started working in magento. i found so many problem in custom development in
magento. i tried so many times for google those things. When you are working with customer module
then you need to know everything about magento customer collection.
some time you want to add new customer programmatically on magento store. most of developer
add customers on store using programmically becasue it is so fast and easy for them.
add-new-customer-to-magento-programmitically
Your can try this code to create new customer in magento store.


$website_Id = Mage::app()->getWebsite()->getId();
$store_id = Mage::app()->getStore();
$new_customer = Mage::getModel("customer/customer");
$new_customer   ->setWebsiteId($websiteId)
            ->setStore($store_id)
            ->setFirstname('star')
            ->setLastname('Gennius')
            ->setEmail('stargenius@w3chacking.com')
            ->setTelephone('0987656532')
            ->setStreet('New Delhi')
           ->setCountry('India');
 
try{
    $new_customer->save();
}
catch (Exception $e) {
    Zend_Debug::dump($e->getMessage());
}

If you want to setup auto password then just use this code instead of password in abobe
code.
  $customer->setPassword($customer->generatePassword($pwd_length));
 
If you want to send welcome email to customer then use those lines also
  $customer->setConfirmation(null); //confirmation needed to register?
  $customer->sendNewAccountEmail(); //send confirmation email to customer?
so here is full code
 $website_Id = Mage::app()->getWebsite()->getId();
$store_id = Mage::app()->getStore();
$new_customer = Mage::getModel("customer/customer");
$new_customer   ->setWebsiteId($websiteId)
            ->setStore($store_id)
            ->setFirstname('star')
            ->setLastname('Gennius')
            ->setEmail('stargenius@w3chacking.com')
            ->setTelephone('0987656532')
            ->setStreet('New Delhi')
           ->setCountry('India')
            ->setConfirmation(0)
            ->sendNewAccountEmail(0);
try{
    $new_customer->save();
}
catch (Exception $e) {
    Zend_Debug::dump($e->getMessage());
}

Thank you for reading my tutorial. comments here if you got any error. i will help you ASAP.

Sign up for our newsletter to get Daily Updates

It is main inner container footer text