Magento 2 get Default Billing and Shipping Address of Current Customer

0
409
Magento 2 get Default Billing and Shipping Address of Current Customer

In this article, you going to see how to get Magento 2 get Default Billing and Shipping Address of Current Customer 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 of Current Customer.

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

If Customer is not logged in then you will get and output display customer is not logged in.

To check the login just create the function called getLogin() function in Block file.

Then, You have to just pass the required parameter which is the address with the help of the function called getDefaultShippingAddressHtml() and getDefaultBillingAddressHtml() function.

Call the above function using Block file get 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
$isLoggedin = $block->getLogin(); //output for logged in customer as 'rocky_xyz@example.com'
if($isLoggedin) {
    $shippingAddress = $block->getDefaultShippingAddress();
    //echo "<pre>";print_r($shippingAddress->__toArray());
    $billingAddress = $block->getDefaultBillingAddress();
    //echo "<pre>";print_r($billingAddress->__toArray());
    echo $block->getDefaultShippingAddressHtml($shippingAddress);
    echo $block->getDefaultBillingAddressHtml($billingAddress);
} else {
    echo __('You have not logged in.');
}
?>

Then in the next step is in block file, Pass address.

To get Magento 2 get Default Billing and Shipping Address .

Use function called getDefaultShippingAddressHtml() and getDefaultBillingAddressHtml() function.

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

Below is class path you have to add.

\Magento\Customer\Helper\Session\CurrentCustomerAddress $currentCustomerAddress,
\Magento\Customer\Model\Address\Config $addressConfig,
\Magento\Customer\Model\Address\Mapper $addressMapper,

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

<?php
namespace VendorName\ModuleName\PathtoBlock;

class Addressinfo extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Customer\Helper\Session\CurrentCustomerAddress $currentCustomerAddress,
        \Magento\Customer\Model\Address\Config $addressConfig,
        \Magento\Customer\Model\Address\Mapper $addressMapper,
        \Magento\Framework\App\Http\Context $httpContext,
        array $data = []
    ) {
        $this->currentCustomerAddress = $currentCustomerAddress;
        $this->_addressConfig = $addressConfig;
        $this->addressMapper = $addressMapper;        
        $this->httpContext = $httpContext;
        parent::__construct($context, $data);
    }

    /**
     * Array for Shipping Address
     *
     * @return \Magento\Framework\Phrase|string
     */
    public function getDefaultShippingAddress()
    {
        try {
            $address = $this->currentCustomerAddress->getDefaultShippingAddress();
        } catch (NoSuchEntityException $e) {
            return __('You have not set a default shipping address.');
        }
        return $address;
    }

    /**
     * Array for Billing Address
     *
     * @return \Magento\Framework\Phrase|string
     */
    public function getDefaultBillingAddress()
    {
        try {
            $address = $this->currentCustomerAddress->getDefaultBillingAddress();
        } 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));
    }

    /*
     * return bool
     */
    public function getLogin() {
        return $this->httpContext->isLoggedIn();
    }

}
?>

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 get Order Items Collection by Item Id

Conclusion :

Finally, we have done with the topic completely.

With the help of above code snippet one can do operation for Magento 2 get Default Billing and Shipping Address .

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