{"id":11736,"date":"2020-07-02T10:01:39","date_gmt":"2020-07-02T04:31:39","guid":{"rendered":"https:\/\/www.hiddentechies.com\/blog\/?p=11736"},"modified":"2020-10-09T18:59:14","modified_gmt":"2020-10-09T13:29:14","slug":"magento-2-add-customer-address-programmatically","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-add-customer-address-programmatically\/","title":{"rendered":"Magento 2 &#8211; How to Add Customer Address Programmatically"},"content":{"rendered":"<p>In this post I am going to explain how to add customer address 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 $addressFactory;\r\n    \r\npublic function __construct(\r\n\t\\Magento\\Catalog\\Block\\Product\\Context $context,\r\n\t\\Magento\\Customer\\Model\\AddressFactory $addressFactory,\r\n\tarray $data = []\r\n) {\r\n\t$this-&gt;addressFactory = $addressFactory;\r\n\tparent::__construct($context, $data);\r\n}\r\n\r\npublic function addAddress($customerId) {\r\n\t$firstName = 'John';\r\n\t$lastName = 'Doe';\r\n\t$address = $this-&gt;addressFactory-&gt;create();\r\n\t$address-&gt;setCustomerId($customerId)\r\n\t\t\t-&gt;setFirstname($firstName)\r\n\t\t\t-&gt;setLastname($lastName)\r\n\t\t\t-&gt;setCountryId('US')\r\n\t\t\t-&gt;setRegionId('9')\r\n\t\t\t-&gt;setPostcode('12345')\r\n\t\t\t-&gt;setCity('City')\r\n\t\t\t-&gt;setTelephone('1234567890')\r\n\t\t\t-&gt;setFax('1234567890')\r\n\t\t\t-&gt;setCompany('Company Name')\r\n\t\t\t-&gt;setStreet(array(\r\n\t\t\t\t'0' =&gt; 'Address Line 1 Text', \/\/ Required\r\n\t\t\t\t'1' =&gt; 'Address Line 2 Text'  \/\/ optional\r\n\t\t\t))\r\n\t\t\t-&gt;setIsDefaultBilling('1')\r\n\t\t\t-&gt;setIsDefaultShipping('1')\r\n\t\t\t-&gt;setSaveInAddressBook('1');\r\n\ttry {\r\n\t\t$address-&gt;save();\r\n\t\treturn __('Address added successfully');\r\n\t} catch (Exception $e) {\r\n\t\treturn __('We can\\'t save the customer address.');\r\n\t}\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>2. Using Object Manager<\/strong><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true \">$objectManager = \\Magento\\Framework\\App\\ObjectManager::getInstance();\r\n\r\n$customerId = 2;\r\n$firstName = 'John';\r\n$lastName = 'Doe';\r\n\r\n$address = $objectManager-&gt;get('\\Magento\\Customer\\Model\\AddressFactory')-&gt;create();\r\n$address-&gt;setCustomerId($customerId)\r\n        -&gt;setFirstname($firstName)\r\n        -&gt;setLastname($lastName)\r\n        -&gt;setCountryId('US')\r\n        -&gt;setRegionId('9')\r\n        -&gt;setPostcode('12345')\r\n        -&gt;setCity('City')\r\n        -&gt;setTelephone('1234567890')\r\n        -&gt;setFax('1234567890')\r\n        -&gt;setCompany('Company Name')\r\n        -&gt;setStreet(array(\r\n            '0' =&gt; 'Address Line 1 Text', \/\/ Required\r\n            '1' =&gt; 'Address Line 2 Text'  \/\/ optional\r\n        ))\r\n        -&gt;setIsDefaultBilling('1')\r\n        -&gt;setIsDefaultShipping('1')\r\n        -&gt;setSaveInAddressBook('1');\r\ntry {\r\n    $address-&gt;save();\r\n    echo __('Address added successfully');\r\n} catch (Exception $e) {\r\n    echo __('We can\\'t save the customer address.');\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>Thats 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=1779494185\" 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 add customer address programmatically in magento 2. 1. Using Dependency Injection Add below code snippet in Block class. protected $addressFactory; public function __construct( \\Magento\\Catalog\\Block\\Product\\Context $context, \\Magento\\Customer\\Model\\AddressFactory $addressFactory, array $data = [] ) { $this-&gt;addressFactory = $addressFactory; parent::__construct($context, $data); } public function addAddress($customerId) { $firstName =&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-add-customer-address-programmatically\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":11822,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[181,59,2138],"tags":[10,27],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/11736"}],"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=11736"}],"version-history":[{"count":1,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/11736\/revisions"}],"predecessor-version":[{"id":11737,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/11736\/revisions\/11737"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/11822"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=11736"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=11736"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=11736"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}