How to Get Customer Addresses by Customer ID in Magento 2
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 implementing guest checkout and then automatically converting them to registered customers, get the customer ID, without relying on session data as it is very necessary. For that, you’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.
To avoid that, you can get customer addresses by customer ID in Magento 2 store and then check for duplicate values.
The below solution helps you collect all customer addresses using customer ID:
Method to Get Customer Addresses by Customer ID in Magento 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
<?php namespace [Vendor]\[Module]\Helper; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Framework\App\Helper\Context; use Magento\Store\Model\StoreManagerInterface; use Magento\Customer\Model\CustomerFactory; class Data extends AbstractHelper { private $storeManager; private $customerFactory; public function __construct( Context $context, StoreManagerInterface $storeManager, CustomerFactory $customerFactory ) { $this->storeManager = $storeManager; $this->customerFactory = $customerFactory; parent::__construct($context); } public function getCustomerAddress($customerId) { $customer = $this->customerFactory->create(); $websiteId = $this->storeManager->getStore()->getWebsiteId(); $customer->setWebsiteId($websiteId); $customerModel = $customer->load($customerId); $customerAddressData = []; $customerAddress = []; if ($customerModel->getAddresses() != null) { foreach ($customerModel->getAddresses() as $address) { $customerAddress[] = $address->toArray(); } } if ($customerAddress != null) { foreach ($customerAddress as $customerAddres) { $street = $customerAddres['street']; $city = $customerAddres['city']; $region = $customerAddres['region']; $country = $customerAddres['country_id']; $postcode = $customerAddres['postcode']; $customerAddressData[] = $customerAddres->toArray(); } } return $customerAddressData; } } |
For example : $address = $this->myHelper->getCustomerAddress(5);
That’s it.
Any doubts in the implementation can be mentioned in the Comments section below and I’d be glad to help you out.
Do share the solution with fellow developers via social media.
Thanks.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
How to Add and edit Footer Links in Magento 2
How to Remove Breadcrumbs From Product Page in Magento 2
Next