Magento 2 get customer address

Today we discuss how in Magento 2 get customer address. Which include the default shipping address and default billing address also the all customer address list too. There are two types of addresses related to customer shipping and billing. you can add the unlimited address as a customer. let start with our example.

Customer Default Billing Address

Get the customer defaut billing address.

namespace QaisarSatti/HelloWorld/Block;

use Magento\Backend\App\Action;

class Delete extends Action
{

    protected $customerFactory;
    protected $addressFactory;

    public function __construct(
        \Magento\Customer\Model\CustomerFactory $customerFactory,
        \Magento\Customer\Model\AddressFactory $addressFactory
    )
    {
        $this->customerFactory = $customerFactory;
        $this->addressFactory = $addressFactory;
    }

    public function getCustomerDefaultBillingAddress()
    {
        $customerObject = $this->customerFactory->create()->load(10);  
        $billingAddressId = $customerObject->getDefaultBilling();
        return $billingAddress = $this->addressFactory->create()->load($billingAddressId);
       
    }
}

Customer Default Shipping Address

Get the customer defaut billing address.

namespace QaisarSatti/HelloWorld/Block;

use Magento\Backend\App\Action;

class Delete extends Action
{

    protected $customerFactory;
    protected $addressFactory;

    public function __construct(
        \Magento\Customer\Model\CustomerFactory $customerFactory,
        \Magento\Customer\Model\AddressFactory $addressFactory
    )
    {
        $this->customerFactory = $customerFactory;
        $this->addressFactory = $addressFactory;
    }

    public function getCustomerDefaultBillingAddress()
    {
        $customerObject = $this->customerFactory->create()->load(10);  
        $shippingAddressId = $customerObject->getDefaultShipping();
        return $shippingAddress = $this->addressFactory->create()->load($shippingAddressId);
       
    }
}

Customer All Address

Get the customer all address.

namespace QaisarSatti/HelloWorld/Block;

use Magento\Backend\App\Action;

class Delete extends Action
{

    protected $customerFactory;
    protected $addressFactory;

    public function __construct(
        \Magento\Customer\Model\CustomerFactory $customerFactory,
        \Magento\Customer\Model\AddressFactory $addressFactory
    )
    {
        $this->customerFactory = $customerFactory;
        $this->addressFactory = $addressFactory;
    }

    public function getCustomerDefaultBillingAddress()
    {
        $customerObject = $this->customerFactory->create()->load(10);  
        return $customerObject->getAddresses();
       
    }
}

Qaisar Satti

Hi, I'm Qaisar Satti! I've been a developer for over 20 years, and now I love sharing what I've learned through tutorials and guides. Whether you're working with Magento, PrestaShop, or WooCommerce, my goal is to make your development journey a bit easier and more fun. When I'm not coding or writing, you can find me exploring new tech trends and hanging out with the amazing developer community. Thanks for stopping by, and happy coding!

Leave a Reply