Monday, July 4, 2022
HomeMagentoMagento 2 get Default Billing and Shipping Address by Customer Id

Magento 2 get Default Billing and Shipping Address by Customer Id

In this article, you going to see how to get Magento 2 get Default Billing and Shipping Address by Customer Id in a detail and a step by step.

If you are looking for this topic then you are at a right place.

Lets start with the topic Magento 2 get Default Billing and Shipping Address.

To get Magento 2 get Default Billing and Shipping Address is very simple.

You have to just pass the required parameter which is the customer id with the help of the function called getDefaultShippingAddress() and getDefaultBillingAddress() function.

Call the above function using the Block file to get the customer’s default billing and shipping address.

Below is a code snippet you have to add in the template file to get all the details of getting customers default billing and shipping address.

<?php
$customerId = 23;

$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);
?>

Then in the next step is in block file, Pass customer Id.

To get Magento 2 get Default Billing and Shipping Address by Customer Id.

Use a function called getDefaultShippingAddress() and getDefaultBillingAddress() function.

For get customer all the details by using its id you have to use the customer class model for address and Interface.

Below is class path you have to add.

\Magento\Customer\Api\AccountManagementInterface $accountManagement,
\Magento\Customer\Model\Address\Config $addressConfig,
\Magento\Customer\Model\Address\Mapper $addressMapper,

Use the below code snippet to get the Default Billing and Shipping Address.

<?php
namespace VendorName\ModuleName\PathtoBlock;

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

After adding the code in the block file, execute the below command to get out put.

php bin/magento indexer:reindex
php bin/magento cache:flush

For final cross-check, go to Admin panel, log in your admin panel.

Login using username and password, Click on Customers Tab -> All Customer

In the end, you will get the default billing Address and shipping Address.

Magento 2 create a customer programmatically

Conclusion :

Finally, we have done with the topic completely.

With the help of the above code snippet, one can do the operation for getting Default Billing and Shipping Address by Customer Id.

I hope you like this article and if you have any queries, please comment below.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular