How-to-get-Default-billing-and-Shipping-address-by-customer-id-in-magento-2

How to Get Default Billing and Shipping Address by Customer ID in Magento 2?

To get the default billing and shipping address of a customer by customer ID, all it requires is just a piece of code. In today’s tutorial, we will guide you with the steps to get the default shipping and billing address by customer ID in Magento 2.

With the help of below code snippet and using a block file, one can get it:

<?php
namespace MD\Customer\Block;
class CustomerAddressById extends \Magento\Framework\View\Element\Template
{
 public function __construct(
 \Magento\Framework\View\Element\Template\Context $context,
 \Magento\Customer\Api\AccountManagementInterface $accountManagement,
 \Magento\Customer\Model\Address\Config $addressConfig,
 \Magento\Customer\Model\Address\Mapper $addressMapper,
 \Magento\Framework\App\Http\Context $httpContext,
 array $data = []
 ) {
 $this->accountManagement = $accountManagement;
 $this->_addressConfig = $addressConfig;
 $this->addressMapper = $addressMapper;
 parent::__construct($context, $data);
 }
/**
 * $customerId
 */
 public function getDefaultShippingAddress($customerId)
 {
 try {
 $address = $this->accountManagement->getDefaultBillingAddress($customerId);
 } catch (NoSuchEntityException $e) {
 return __('You have not set a default shipping address.');
 }
 return $address;
 }
/**
 * $customerId
 */
 public function getDefaultBillingAddress($customerId)
 {
 try {
 $address = $this->accountManagement->getDefaultBillingAddress($customerId);
 } catch (NoSuchEntityException $e) {
 return __('You have not set a default billing address.');
 }
 return $address;
 }
 /* Html Format */
 public function getDefaultShippingAddressHtml($address) {
 if ($address) {
 return $this->_getAddressHtml($address);
 } else {
 return __('You have not set a default Shipping address.');
 }
 }
 /* Html Format */
 public function getDefaultBillingAddressHtml($address) {
 if ($address) {
 return $this->_getAddressHtml($address);
 } else {
 return __('You have not set a default billing address.');
 }
 }

 /**
 * Render an address as HTML and return the result
 *
 * @param AddressInterface $address
 * @return string
 */
 protected function _getAddressHtml($address)
 {
 /** @var \Magento\Customer\Block\Address\Renderer\RendererInterface $renderer */
 $renderer = $this->_addressConfig->getFormatByCode('html')->getRenderer();
 return $renderer->renderArray($this->addressMapper->toFlatArray($address));
 }
}

Now, Call the required function in the template file,

<?php
$customerId = 25;
$shippingAddress = $block->getDefaultShippingAddress($customerId);
//echo "<pre>";print_r($shippingAddress->__toArray());
$billingAddress = $block->getDefaultBillingAddress($customerId);
//echo "<pre>";print_r($billingAddress->__toArray());
echo $block->getDefaultShippingAddressHtml($shippingAddress);
echo $block->getDefaultBillingAddressHtml($billingAddress);
?>

Here you go!

You’ve got everything you might require, to get the default billing and shipping address in Magento 2. Feel free to reach us out if we missed out anything.

Tags