🔥 Just Launched! Werra Premium Template for HyväSee it in Action

How to Get Customer Addresses by Customer ID in Magento 2

By Sanjay JethvaUpdated on May 22, 2025 2 min read

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:

<?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;
    }
}

Note: Using [Vendor]\[Module]\Helper class’s  object you can call method getCustomerAddress(id) to get Address Data

For example : $address = $this->myHelper->getCustomerAddress(5);

That’s it.

Do share the solution with fellow developers via social media.

Thanks.

Sanjay Jethva Full Image
Article bySanjay 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.