{"id":11734,"date":"2020-06-25T09:40:30","date_gmt":"2020-06-25T04:10:30","guid":{"rendered":"https:\/\/www.hiddentechies.com\/blog\/?p=11734"},"modified":"2020-10-09T18:59:39","modified_gmt":"2020-10-09T13:29:39","slug":"magento-2-create-customer-programmatically","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento\/magento-2-create-customer-programmatically\/","title":{"rendered":"Magento 2 &#8211; How to Create Customer Programmatically"},"content":{"rendered":"<p>In this post I am going to explain how to create customer programmatically in magento 2.<\/p>\n<p><strong>1. Using Dependency Injection<\/strong><\/p>\n<p>Add below code snippet in Block class.<\/p>\n<pre class=\"lang:default decode:true\">protected $storeManager;\r\nprotected $customerFactory;\r\n\r\npublic function __construct(\r\n\t\\Magento\\Catalog\\Block\\Product\\Context $context,\r\n\t\\Magento\\Store\\Model\\StoreManagerInterface $storeManager,\r\n\t\\Magento\\Customer\\Model\\CustomerFactory $customerFactory,\r\n\tarray $data = []\r\n) {\r\n\t$this-&gt;storeManager = $storeManager;\r\n\t$this-&gt;customerFactory = $customerFactory;\r\n\tparent::__construct($context, $data);\r\n}\r\n\r\npublic function addCustomer($firstName, $lastName, $email, $password) \r\n{\r\n\t$store = $this-&gt;storeManager-&gt;getStore();\r\n\t$websiteId = $this-&gt;storeManager-&gt;getWebsite()-&gt;getWebsiteId();\r\n\r\n\t$customer = $this-&gt;customerFactory-&gt;create();\r\n\t\r\n\t\/\/ Set website ID\r\n\t$customer-&gt;setWebsiteId($websiteId);\r\n\t\r\n\t\/\/ Set Store\r\n\t$customer-&gt;setStore($store);\r\n\r\n\t\/\/ Check if customer is already exists\r\n\tif ($customer-&gt;loadByEmail($email)-&gt;getId()) {\r\n\t\treturn __('There is already an account with this email address \"%1\".', $email);\r\n\t} else {\r\n\t\t$customer-&gt;setEmail($email);\r\n\t\t$customer-&gt;setFirstname($firstName);\r\n\t\t$customer-&gt;setLastname($lastName);\r\n\t\t$customer-&gt;setPassword($password);\r\n\t\t$customer-&gt;setForceConfirmed(true);\r\n\r\n\t\t\/\/ Save customer data\r\n\t\t$customer-&gt;save();\r\n\r\n\t\t\/\/ Send email\r\n\t\t$customer-&gt;sendNewAccountEmail();\r\n\r\n\t\treturn __('Customer account with email %1 created successfully.', $email);\r\n\t}\r\n}<\/pre>\n<p><strong>2. Using Object Manager<\/strong><\/p>\n<pre class=\"lang:default decode:true\">$objectManager = \\Magento\\Framework\\App\\ObjectManager::getInstance();\r\n\r\n$storeManager = $objectManager-&gt;get('\\Magento\\Store\\Model\\StoreManagerInterface');\r\n\r\n$websiteId = $storeManager-&gt;getStore()-&gt;getWebsiteId();\r\n$store = $storeManager-&gt;getStore();\r\n\r\n$firstName = 'John';\r\n$lastName = 'Doe';\r\n$email = 'johndoe2@yourdomain.com';\r\n$password = 'John@1234';\r\n\r\n$customer = $objectManager-&gt;get('\\Magento\\Customer\\Model\\CustomerFactory')-&gt;create();\r\n\r\n\/\/ Set website ID\r\n$customer-&gt;setWebsiteId($websiteId);\r\n\r\n\/\/ Set Store\r\n$customer-&gt;setStore($store);\r\n\r\n\/\/ Check if customer is already exists\r\nif ($customer-&gt;loadByEmail($email)-&gt;getId()) {\r\n    echo 'Customer with email ' . $email . ' is already registered.';\r\n} else {\r\n    $customer-&gt;setEmail($email);\r\n    $customer-&gt;setFirstname($firstName);\r\n    $customer-&gt;setLastname($lastName);\r\n    $customer-&gt;setPassword($password);\r\n    $customer-&gt;setForceConfirmed(true);\r\n\r\n    \/\/ Save customer data\r\n    $customer-&gt;save();\r\n\r\n    \/\/ Send email\r\n    $customer-&gt;sendNewAccountEmail();\r\n\r\n    echo 'Customer with the email ' . $email . ' is successfully created.';\r\n}<\/pre>\n<p><strong>Note: After creating customer you need to run indexer command to see a customer in the backend grid.<\/strong><\/p>\n<pre class=\"lang:default decode:true\">php bin\/magento indexer:reindex\r\nphp bin\/magento cache:flush<\/pre>\n<p>That&#8217;s it. Enjoy Magento 2!!<\/p>\n<div class=\"angwp_12010 _ning_cont _ning_hidden _ning_outer _align_center responsive\" data-size=\"custom\" data-bid=\"12010\" data-aid=\"0\" style=\"max-width:800px; width:100%;height:inherit;\"><div class=\"_ning_label _left\" style=\"\"><\/div><div class=\"_ning_inner\" style=\"\"><a href=\"https:\/\/www.hiddentechies.com\/blog?_dnlink=12010&t=1775821737\" class=\"strack_cli _ning_link\" target=\"_blank\">&nbsp;<\/a><div class=\"_ning_elmt\"><img decoding=\"async\" src=\"https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/angwp\/items\/12010\/Banner-2.png\" \/><\/div><\/div><\/div><div class=\"clear\"><\/div>","protected":false},"excerpt":{"rendered":"<p>In this post I am going to explain how to create customer programmatically in magento 2. 1. Using Dependency Injection Add below code snippet in Block class. protected $storeManager; protected $customerFactory; public function __construct( \\Magento\\Catalog\\Block\\Product\\Context $context, \\Magento\\Store\\Model\\StoreManagerInterface $storeManager, \\Magento\\Customer\\Model\\CustomerFactory $customerFactory, array $data = [] ) { $this-&gt;storeManager = $storeManager; $this-&gt;customerFactory = $customerFactory; parent::__construct($context, $data); }&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento\/magento-2-create-customer-programmatically\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":11820,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[181,59,2138],"tags":[2127,10,27],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/11734"}],"collection":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/comments?post=11734"}],"version-history":[{"count":5,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/11734\/revisions"}],"predecessor-version":[{"id":11783,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/11734\/revisions\/11783"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/11820"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=11734"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=11734"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=11734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}