{"id":22307,"date":"2018-10-29T12:52:55","date_gmt":"2018-10-29T12:52:55","guid":{"rendered":"https:\/\/www.knowband.com\/blog\/?p=22307\/"},"modified":"2018-11-23T17:42:57","modified_gmt":"2018-11-23T07:12:57","slug":"create-customer-in-magento","status":"publish","type":"post","link":"https:\/\/www.knowband.com\/blog\/magento-tutorials\/create-customer-in-magento\/","title":{"rendered":"How to programmatically create customer in Magento?"},"content":{"rendered":"<p>Magento is one of the most flexible and widely used e-commerce platforms nowadays. Many times during our development we need to create customers via our code. For example, if we are developing a Magento plugin to connect our Magento store to marketplaces like Walmart, Etsy etc then we create an order for the customer via our code. But to create an order we need to create customer if that customer does not exist on our Magento store. We can create customer in Magento simply by using the following code.<\/p>\n<h2>Code for creating a customer in Magento:<\/h2>\n<pre class=\"brush: actionscript3; gutter: true\">$_website = Mage::app()-&gt;getWebsite()--&gt;getId();\r\n        $store = Mage::app()-&gt;getStore();       \r\n        $_firstName = &#039;Jhon&#039;;\r\n        $_lastName = &#039;Doe&#039;;\r\n        $_customerEmail = &#039;demo@demo.com&#039;;\r\n        $_customerPassword = &#039;#KnowbandPlugins&#039;;\r\n        \r\n        $_newCustomer = Mage::getModel(&quot;customer\/customer&quot;);\r\n        $_newCustomer-&gt;setWebsiteId($_website)\r\n                     -&gt;setStore($store)\r\n                     -&gt;setFirstname($_firstName)\r\n                     -&gt;setLastname($_lastName)\r\n                     -&gt;setEmail($_customerEmail)\r\n                     -&gt;setPassword($_customerPassword);\r\n        try{\r\n            $_newCustomer-&gt;save();\r\n        }\r\n        catch (Exception $e) {\r\n            Mage::getSingleton(&#039;customer\/session&#039;)-&gt;addException($e, $this-&gt;__(&#039;Error in creating customer&#039;));\r\n            Mage::logException($e);\r\n        }\r\n&lt;a href=&quot;https:\/\/www.knowband.com\/blog\/wp-content\/uploads\/2018\/10\/untitled-26.png&quot;&gt;&lt;img class=&quot;alignnone size-medium wp-image-22306&quot; src=&quot;https:\/\/www.knowband.com\/blog\/wp-content\/uploads\/2018\/10\/untitled-26-300x48.png&quot; alt=&quot;creating-customer-in-magento&quot; width=&quot;300&quot; height=&quot;48&quot; \/&gt;&lt;\/a&gt;<\/pre>\n<p>But this customer only has very basic information like first name, last name, email etc. It does not have shipping or billing address. We can save complete address information of the customer via the following code.<\/p>\n<h2>Code for saving the address of the customer:<\/h2>\n<pre class=\"brush: actionscript3; gutter: true\">$_website = Mage::app();getWebsite();\r\n$_customerEmail = &#039;demo@demo.com&#039;; \/\/Customer Email\r\n$_countryId = &#039;IN&#039;;\r\n        $_postCode = &#039;201301&#039;;\r\n        $_city = &#039;Noida&#039;;\r\n        $_telephone = &#039;1111111111&#039;;\r\n        $_fax= &#039;&#039;;\r\n        $_company = &#039;Velocity Software Solutions&#039;;\r\n        $_street = &#039;&#039;;\r\n        \r\n        $_customer = Mage::getModel(&#039;customer\/customer&#039;)\r\n                -&gt;setWebsiteId($_website-&gt;;getId())\r\n                -&gt;loadByEmail($_customerEmail);\r\n        $_customerAddress = Mage::getModel(&quot;customer\/address&quot;);\r\n        $_customerAddress-&gt;setCustomerId($_customer-&gt;getId())\r\n                -&gt;setFirstname($_customer-&gt;getFirstname())\r\n                -&gt;setMiddleName($_customer-&gt;getMiddlename())\r\n                -&gt;setLastname($_customer-&gt;getLastname())\r\n                -&gt;setCountryId($_countryId)\r\n                -&gt;setPostcode($_postCode)\r\n                -&gt;setCity($_city)\r\n                -&gt;setTelephone($_telephone)\r\n                -&gt;setFax($_fax)\r\n                -&gt;setCompany($_company)\r\n                -&gt;setStreet($_street)\r\n                -&gt;setIsDefaultBilling(&#039;1&#039;)\r\n                -&gt;setIsDefaultShipping(&#039;1&#039;)\r\n                -&gt;setSaveInAddressBook(&#039;1&#039;);\r\n\r\n        try{\r\n            $_customerAddress-&gt;save();\r\n        }\r\n        catch (Exception $e) {\r\n            Mage::getSingleton(&#039;customer\/session&#039;)-&gt;addException($e, $this-&gt;__(&#039;Error in saving customer address.&#039;));\r\n            Mage::logException($e);\r\n        }\r\n&lt;a href=&quot;https:\/\/www.knowband.com\/blog\/wp-content\/uploads\/2018\/10\/untitled-27.png&quot;&gt;&lt;img class=&quot;alignnone size-medium wp-image-22308&quot; src=&quot;https:\/\/www.knowband.com\/blog\/wp-content\/uploads\/2018\/10\/untitled-27-300x133.png&quot; alt=&quot;saving-address-of-customer&quot; width=&quot;300&quot; height=&quot;133&quot; \/&gt;&lt;\/a&gt;<\/pre>\n<p>Now the customer address information is complete. Hope the above-mentioned code will help you in adding a new customer to your Magento store.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento is one of the most flexible and widely used e-commerce platforms nowadays. Many times during our development we need to create customers via our code. For example, if we are developing a Magento plugin to connect our Magento store to marketplaces like Walmart, Etsy etc then we create an order for the customer via [&hellip;]<\/p>\n","protected":false},"author":14,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_kbui_enabled":"","footnotes":""},"categories":[6],"tags":[7500,7501,1555,1573,97],"class_list":["post-22307","post","type-post","status-publish","format-standard","hentry","category-magento-tutorials","tag-create-customer-in-magento","tag-create-customer-programmatically","tag-magento-extension","tag-magento-module","tag-magento-store"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to programmatically create customer in Magento? - Knowband Blog<\/title>\n<meta name=\"description\" content=\"Following technical write-up is all about the easy way to create customer in Magento stores with few program codes. Hopefully, it would be helpful.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.knowband.com\/blog\/magento-tutorials\/create-customer-in-magento\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to programmatically create customer in Magento? - Knowband Blog\" \/>\n<meta property=\"og:description\" content=\"Following technical write-up is all about the easy way to create customer in Magento stores with few program codes. Hopefully, it would be helpful.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.knowband.com\/blog\/magento-tutorials\/create-customer-in-magento\/\" \/>\n<meta property=\"og:site_name\" content=\"Knowband Blog | Ecommerce Modules\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Knowband\" \/>\n<meta property=\"article:published_time\" content=\"2018-10-29T12:52:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-11-23T07:12:57+00:00\" \/>\n<meta name=\"author\" content=\"Prashant Kumar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@KnowbandPlugin\" \/>\n<meta name=\"twitter:site\" content=\"@KnowbandPlugin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prashant Kumar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/magento-tutorials\\\/create-customer-in-magento\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/magento-tutorials\\\/create-customer-in-magento\\\/\"},\"author\":{\"name\":\"Prashant Kumar\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#\\\/schema\\\/person\\\/04499d0d3f542a4f42182b91f772986c\"},\"headline\":\"How to programmatically create customer in Magento?\",\"datePublished\":\"2018-10-29T12:52:55+00:00\",\"dateModified\":\"2018-11-23T07:12:57+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/magento-tutorials\\\/create-customer-in-magento\\\/\"},\"wordCount\":170,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#organization\"},\"keywords\":[\"create customer in magento\",\"create customer programmatically\",\"Magento extension\",\"Magento module\",\"Magento Store\"],\"articleSection\":[\"Magento Tutorials\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/magento-tutorials\\\/create-customer-in-magento\\\/\",\"url\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/magento-tutorials\\\/create-customer-in-magento\\\/\",\"name\":\"How to programmatically create customer in Magento? - Knowband Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#website\"},\"datePublished\":\"2018-10-29T12:52:55+00:00\",\"dateModified\":\"2018-11-23T07:12:57+00:00\",\"description\":\"Following technical write-up is all about the easy way to create customer in Magento stores with few program codes. Hopefully, it would be helpful.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/magento-tutorials\\\/create-customer-in-magento\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.knowband.com\\\/blog\\\/magento-tutorials\\\/create-customer-in-magento\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/magento-tutorials\\\/create-customer-in-magento\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to programmatically create customer in Magento?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#website\",\"url\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/\",\"name\":\"Knowband Blog | Ecommerce Modules\",\"description\":\"Magento, Prestashop, Opencart, Shopify\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#organization\",\"name\":\"Knowband Blog | Ecommerce Modules\",\"url\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/wp-content\\\/uploads\\\/2016\\\/09\\\/cropped-knowband_logo-v2.png\",\"contentUrl\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/wp-content\\\/uploads\\\/2016\\\/09\\\/cropped-knowband_logo-v2.png\",\"width\":153,\"height\":61,\"caption\":\"Knowband Blog | Ecommerce Modules\"},\"image\":{\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/Knowband\",\"https:\\\/\\\/x.com\\\/KnowbandPlugin\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#\\\/schema\\\/person\\\/04499d0d3f542a4f42182b91f772986c\",\"name\":\"Prashant Kumar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/10\\\/10256082_607442326014813_1731639576902538685_o-150x150.jpg\",\"url\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/10\\\/10256082_607442326014813_1731639576902538685_o-150x150.jpg\",\"contentUrl\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/10\\\/10256082_607442326014813_1731639576902538685_o-150x150.jpg\",\"caption\":\"Prashant Kumar\"},\"description\":\"Prashant is a passionate Magento developer. He loves exploring e-commerce and reading books.\",\"url\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/author\\\/pkumar\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to programmatically create customer in Magento? - Knowband Blog","description":"Following technical write-up is all about the easy way to create customer in Magento stores with few program codes. Hopefully, it would be helpful.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.knowband.com\/blog\/magento-tutorials\/create-customer-in-magento\/","og_locale":"en_US","og_type":"article","og_title":"How to programmatically create customer in Magento? - Knowband Blog","og_description":"Following technical write-up is all about the easy way to create customer in Magento stores with few program codes. Hopefully, it would be helpful.","og_url":"https:\/\/www.knowband.com\/blog\/magento-tutorials\/create-customer-in-magento\/","og_site_name":"Knowband Blog | Ecommerce Modules","article_publisher":"https:\/\/www.facebook.com\/Knowband","article_published_time":"2018-10-29T12:52:55+00:00","article_modified_time":"2018-11-23T07:12:57+00:00","author":"Prashant Kumar","twitter_card":"summary_large_image","twitter_creator":"@KnowbandPlugin","twitter_site":"@KnowbandPlugin","twitter_misc":{"Written by":"Prashant Kumar","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.knowband.com\/blog\/magento-tutorials\/create-customer-in-magento\/#article","isPartOf":{"@id":"https:\/\/www.knowband.com\/blog\/magento-tutorials\/create-customer-in-magento\/"},"author":{"name":"Prashant Kumar","@id":"https:\/\/www.knowband.com\/blog\/fr\/#\/schema\/person\/04499d0d3f542a4f42182b91f772986c"},"headline":"How to programmatically create customer in Magento?","datePublished":"2018-10-29T12:52:55+00:00","dateModified":"2018-11-23T07:12:57+00:00","mainEntityOfPage":{"@id":"https:\/\/www.knowband.com\/blog\/magento-tutorials\/create-customer-in-magento\/"},"wordCount":170,"commentCount":0,"publisher":{"@id":"https:\/\/www.knowband.com\/blog\/fr\/#organization"},"keywords":["create customer in magento","create customer programmatically","Magento extension","Magento module","Magento Store"],"articleSection":["Magento Tutorials"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.knowband.com\/blog\/magento-tutorials\/create-customer-in-magento\/","url":"https:\/\/www.knowband.com\/blog\/magento-tutorials\/create-customer-in-magento\/","name":"How to programmatically create customer in Magento? - Knowband Blog","isPartOf":{"@id":"https:\/\/www.knowband.com\/blog\/fr\/#website"},"datePublished":"2018-10-29T12:52:55+00:00","dateModified":"2018-11-23T07:12:57+00:00","description":"Following technical write-up is all about the easy way to create customer in Magento stores with few program codes. Hopefully, it would be helpful.","breadcrumb":{"@id":"https:\/\/www.knowband.com\/blog\/magento-tutorials\/create-customer-in-magento\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.knowband.com\/blog\/magento-tutorials\/create-customer-in-magento\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.knowband.com\/blog\/magento-tutorials\/create-customer-in-magento\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.knowband.com\/blog\/fr\/"},{"@type":"ListItem","position":2,"name":"How to programmatically create customer in Magento?"}]},{"@type":"WebSite","@id":"https:\/\/www.knowband.com\/blog\/fr\/#website","url":"https:\/\/www.knowband.com\/blog\/fr\/","name":"Knowband Blog | Ecommerce Modules","description":"Magento, Prestashop, Opencart, Shopify","publisher":{"@id":"https:\/\/www.knowband.com\/blog\/fr\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.knowband.com\/blog\/fr\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.knowband.com\/blog\/fr\/#organization","name":"Knowband Blog | Ecommerce Modules","url":"https:\/\/www.knowband.com\/blog\/fr\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.knowband.com\/blog\/fr\/#\/schema\/logo\/image\/","url":"https:\/\/www.knowband.com\/blog\/wp-content\/uploads\/2016\/09\/cropped-knowband_logo-v2.png","contentUrl":"https:\/\/www.knowband.com\/blog\/wp-content\/uploads\/2016\/09\/cropped-knowband_logo-v2.png","width":153,"height":61,"caption":"Knowband Blog | Ecommerce Modules"},"image":{"@id":"https:\/\/www.knowband.com\/blog\/fr\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/Knowband","https:\/\/x.com\/KnowbandPlugin"]},{"@type":"Person","@id":"https:\/\/www.knowband.com\/blog\/fr\/#\/schema\/person\/04499d0d3f542a4f42182b91f772986c","name":"Prashant Kumar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.knowband.com\/blog\/wp-content\/uploads\/2018\/10\/10256082_607442326014813_1731639576902538685_o-150x150.jpg","url":"https:\/\/www.knowband.com\/blog\/wp-content\/uploads\/2018\/10\/10256082_607442326014813_1731639576902538685_o-150x150.jpg","contentUrl":"https:\/\/www.knowband.com\/blog\/wp-content\/uploads\/2018\/10\/10256082_607442326014813_1731639576902538685_o-150x150.jpg","caption":"Prashant Kumar"},"description":"Prashant is a passionate Magento developer. He loves exploring e-commerce and reading books.","url":"https:\/\/www.knowband.com\/blog\/author\/pkumar\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/posts\/22307","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/comments?post=22307"}],"version-history":[{"count":4,"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/posts\/22307\/revisions"}],"predecessor-version":[{"id":22864,"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/posts\/22307\/revisions\/22864"}],"wp:attachment":[{"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/media?parent=22307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/categories?post=22307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/tags?post=22307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}