How to Create Customer programmatically in Magento?



In this post I just want to describe how we can create new customer programmatically in Magento, Even though anyone can create new customer via admin interface or through a signup form on the website, but in some cases if you have numbers of the customer then this will take alot of time to create the customer so this will be the time consuming task, to avoid this situation, I have created a script by using it you can easily create the customer in Magento in very less time.

Suppose you want to create the new customer via using the Code in Magento, its very easy and you can create the customer from the steps given below:

$siteId = Mage::app()->getWebsite()->getId();
$store = Mage::app()->getStore();
 
$customer_data = Mage::getModel("customer/customer");
$customer_data   ->setWebsiteId($siteId)
					->setStore($store)
					->setGroupId(2)
					->setPrefix('Sir')
					->setFirstname('EWA')
					->setMiddleName('2')
					->setLastname('Developer')
					->setSuffix('II')
					->setEmail('expertwebadvisor@gmail.com')
					->setPassword('amdin@123');
 
try{
    $customer_data->save();
}
catch (Exception $e) {
    Zend_Debug::dump($e->getMessage());
}

By using the above code you can successfully create the user and if you want to assign the address to this customer just follow the step given below:

$address_data = Mage::getModel("customer/address");
$address_data 	->setCustomerId($customer_data->getId())
				->setFirstname($customer_data->getFirstname())
				->setMiddleName($customer_data->getMiddlename())
				->setLastname($customer_data->getLastname())
				//->setRegionId('1') //state/province, only needed country is USA
				->setPostcode('90001')
				->setCity('Alaska')
				->setTelephone('032845785')
				->setFax('032845785')
				->setCompany('EWA')
				->setStreet('Chakresia')
				->setIsDefaultBilling('1')
				->setIsDefaultShipping('1')
				->setSaveInAddressBook('1');
 
try{
    $address_data->save();
}
catch (Exception $e) {
    Zend_Debug::dump($e->getMessage());
}

Now the customer with the address, so now this customer is ready for the order.

Stay tuned for the Magento and Magento2 latest tutorial and updates, Hope you enjoyed reading, if you need the professional Magento Development we can help you, just Click on the Link and send me your requirements.