Menu Close
    Searching for an experienced Magento 2 Development Company ?

    Get Customer Address in String format in Magento 2

    Hello Friends!

    In this blog, we are going to learn how we can get an address in string format.

    In the following controller, we have loaded an address by id and got the string format of that address.

    <?php
    /**
     * Vendor Desc.
     *
     * @category  Vendor
     * @package   Vendor_CustomModule
     * @author    Vendor
     * @copyright Copyright (c) Vendor
     * @license   https://example.com/license.html
     */
    namespace Vendor\CustomModule\Controller\Demo;
    
    use Magento\Framework\App\Action\Action;
    use Magento\Framework\App\Action\Context;
    use Magento\Framework\Controller\ResultFactory;
    
    class GetAddressFormatString extends Action
    {
        /**
         * @var ResultFactory
         */
        protected $resultFactory;
    
        /**
         * initialization
         *
         * @param Context $context
         * @param ResultFactory $resultFactory
         * @param \Magento\Customer\Model\Address\Config $addressConfig
         * @param \Magento\Customer\Model\Address\Mapper $addressMapper
         * @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
         * 
         * @return void
         */
        public function __construct(
            Context $context,
            ResultFactory $resultFactory,
            \Magento\Customer\Model\Address\Config $addressConfig,
            \Magento\Customer\Model\Address\Mapper $addressMapper,
            \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
        ) {
            $this->resultFactory     = $resultFactory;
            $this->addressRepository = $addressRepository;
            $this->addressConfig     = $addressConfig;
            $this->addressMapper     = $addressMapper;
            parent::__construct($context);
        }
        
        /**
         * Execute method to get result
         */
        public function execute()
        {
            $addressId = 1;
            $result = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_RAW);
            $addressString = $this->getFormattedAddressAsStringByAddressId($addressId);
            $result->setHeader('Content-Type','text/html')
                ->setContents('Address: '.$addressString);
    
            return $result;
        }
    
        /**
         * Get Formatted Address as String
         * 
         * @param int $addressId
         * @return string
         */
        public function getFormattedAddressAsStringByAddressId($addressId)
        {
            $addressString = "";
            try {
                $addressObject = $this->addressRepository->getById($addressId);
                /** @var \Magento\Customer\Block\Address\Renderer\RendererInterface $renderer */
                $renderer = $this->addressConfig->getFormatByCode('html')->getRenderer();
                $addressString = $renderer->renderArray(
                    $this->addressMapper->toFlatArray($addressObject)
                );
            } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
                return "";
            }
            return $addressString;
        }
    }

    Now, when we execute this controller on the browser. We will get the result as following image:

    AddressInStringFormat
    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top