Magento how to programmatically create customers


Step 1: Define Function


Magento 1


<?php class MageCheck_Tutorial_IndexController extends Mage_Core_Controller_Front_Action { public function indexAction() { $this-> createCustomer(); } public function createCustomer() { // ... } }

Magento 2


/** * @var \Magento\Customer\Model\Customer $customer */ protected $_customer; public function __construct( \Magento\Customer\Model\Customer $customer ){ $this->_customer = $customer; } public function execute() { $this->createCustomer(); } public function createCustomer() { // ... }

Step 2: Create Function


Magento 1


public function createCustomer() { $websiteId = Mage::app()->getWebsite()->getId(); $store = Mage::app()->getStore(); $_customer = array( 'firstname' => 'Mage', 'lastname' => 'Check', 'email' => 'example@magecheck.com', 'password' => '123456789', 'password_hash' => 'b7a9552802ce455e170a1c73a47796e4:6c', 'add_date' => '2018-11-21', 'birth_date' => '1980-01-01', 'gender' => 1, // Male (1) or Female (2) 'telephone' => '1234567890', ); $customer = \Mage::getModel('customer/customer'); $customer->setWebsiteId($websiteId) ->setStore($store) ->setFirstname($_customer['firstname']) ->setLastname($_customer['lastname']) ->setEmail($_customer['email']) ->setPasswordHash($_customer['password']) ->setCreatedAt($_customer['add_date']) ->setDob($_customer['birth_date']) ->setGender($_customer['gender']); $customer->save(); }

Magento 2


public function createCustomer() { $customer = $this->_customer; $firstName = 'Mage'; $lastName = 'Check'; $customer->setEmail('example@magecheck.com'); $customer->setFirstname($firstName); $customer->setLastname($lastName); $customer->setPassword('123456789'); $customer->save(); }