{"id":633,"date":"2019-10-15T06:16:03","date_gmt":"2019-10-15T06:16:03","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/10\/15\/get-customer-addresses-by-customer-id-magento-2\/"},"modified":"2025-05-22T15:35:30","modified_gmt":"2025-05-22T10:05:30","slug":"get-customer-addresses-by-customer-id-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/get-customer-addresses-by-customer-id-magento-2\/","title":{"rendered":"How to Get Customer Addresses by Customer ID in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">If you want to implement any features in the Magento 2 store based on the customer address, this post is for you.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, while implementing guest checkout and then automatically converting them to registered customers, <a href=\"https:\/\/meetanshi.com\/blog\/get-customer-id-without-using-session-in-magento-2\/\">get the customer ID, without relying on session data<\/a> as it is very necessary. For that, you&#8217;d need the customer address. But what if the already signed up customer uses guest checkout! The duplicate addresses are saved and it will mess up the address database.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To avoid that, you can <em><strong>get customer addresses by customer ID in Magento 2<\/strong><\/em> store and then check for duplicate values.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The below solution helps you collect all customer addresses using customer ID:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Get Customer Addresses by Customer ID in Magento 2:<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php \n\nnamespace [Vendor]\\[Module]\\Helper;\n\nuse Magento\\Framework\\App\\Helper\\AbstractHelper;\nuse Magento\\Framework\\App\\Helper\\Context;\nuse Magento\\Store\\Model\\StoreManagerInterface;\nuse Magento\\Customer\\Model\\CustomerFactory;\n\nclass Data extends AbstractHelper\n{\n    private $storeManager;\n    private $customerFactory;\n\n    public function __construct(\n        Context $context,\n        StoreManagerInterface $storeManager,\n        CustomerFactory $customerFactory\n    ) {\n        $this->storeManager = $storeManager;\n        $this->customerFactory = $customerFactory;\n        parent::__construct($context);\n    }\n\n    public function getCustomerAddress($customerId)\n    {\n        $customer = $this->customerFactory->create();\n        $websiteId = $this->storeManager->getStore()->getWebsiteId();\n        $customer->setWebsiteId($websiteId);\n        $customerModel = $customer->load($customerId);\n\n        $customerAddressData = [];\n        $customerAddress = [];\n\n        if ($customerModel->getAddresses() != null)\n        {\n            foreach ($customerModel->getAddresses() as $address) {\n                $customerAddress[] = $address->toArray();\n            }\n        }\n\n        if ($customerAddress != null)\n        {\n            foreach ($customerAddress as $customerAddres)\n            {\n                $street = $customerAddres['street'];\n                $city = $customerAddres['city'];\n                $region = $customerAddres['region'];\n                $country = $customerAddres['country_id'];\n                $postcode = $customerAddres['postcode'];\n\n                $customerAddressData[] = $customerAddres->toArray();\n            }\n        }\n        return $customerAddressData;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong>&nbsp;Using [Vendor]\\[Module]\\Helper class\u2019s &nbsp;object you can call method getCustomerAddress(id) to get Address Data<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example : $address = $this-&gt;myHelper-&gt;getCustomerAddress(5);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do share the solution with fellow developers via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you want to implement any features in the Magento 2 store based on the customer address, this post is for you. For example, while&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-633","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/633","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=633"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/633\/revisions"}],"predecessor-version":[{"id":15225,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/633\/revisions\/15225"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}