Question

enter image description here

How to get the Customer Address like above format and how to print it, If there is more than one customer address how to get the particular address of the customer.

Thanks in Advance :)

Was it helpful?

Solution

create Model

app/code/VendoreName/ModuleName/Model

CustomerAddress.php

<?php

namespace VendoreName\ModuleName\Model;

use Magento\Framework\Model\AbstractModel;
use VendoreName\ModuleName\Model\ResourceModel\CustomerAddress as CustomerAddressResourceModel;

class CustomerAddress extends AbstractModel
{
    protected function _construct()
    {
        $this->_init(CustomerAddressResourceModel::class);
    }
}

app/code/VendoreName/ModuleName/Model/ResourceModel

CustomerAddress.php

<?php

namespace VendoreName\ModuleName\Model\ResourceModel;

use Magento\Framework\Model\ResourceModel\Db\AbstractDb;

class CustomerAddress extends AbstractDb
{
    protected function _construct()
    {
        $this->_init('customer_address_entity', 'entity_id');
    }
}

app/code/VendoreName/ModuleName/Model/ResourceModel/CustomerAddress

Collection.php

<?php

namespace VendoreName\ModuleName\Model\ResourceModel\CustomerAddress;

use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use VendoreName\ModuleName\Model\CustomerAddress as CustomerAddressModel;
use VendoreName\ModuleName\Model\ResourceModel\CustomerAddress as CustomerAddressResourceModel;

class Collection extends AbstractCollection
{
    protected function _construct()
    {
        $this->_init(
            CustomerAddressModel::class,
            CustomerAddressResourceModel::class
        );
    }
}

And get your customer multiple address

    protected $addressMapper;
    protected $CustomerAddress;

    public function __construct(
        ...........................................................
        \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,
   \VendoreName\ModuleName\Model\ResourceModel\CustomerAddress\CollectionFactory $CustomerAddress,
        ...........................................................
    ) {
        ...........................................................
        $this->currentCustomer = $currentCustomer;
        $this->CustomerAddress = $CustomerAddress;    
        ...........................................................
    }

    public function getMultipleAddress()
    {
        $cust_id = $this->currentCustomer->getCustomerId();
        $data = $this->CustomerAddress->create();
        $data->addFieldToFilter('parent_id',$cust_id);
        return $data;
    }
}

and call in your phtml file

echo "<pre>";
print_r($block->getMultipleAddress()->getData());
exit();

for print like default address please follow magento root/vendor/magento/module-customer/view/frontend/templates/account/dashboard/address.phtml

I Hope This Helps You.

OTHER TIPS

you can go into Store >> Configuration >> Customers >> Customer Configuration >> Address Templates >> HTML.

It is formatted address template you can adjust like below:

{{depend prefix}}{{var prefix}} {{/depend}}{{var firstname}} {{depend middlename}}{{var middlename}} {{/depend}}{{var lastname}}{{depend suffix}} {{var suffix}}{{/depend}}{{depend firstname}}<br />{{/depend}}
{{depend company}}{{var company}}<br />{{/depend}}
{{if street1}}{{var street1}}<br />{{/if}}
{{depend street2}}{{var street2}}<br />{{/depend}}
{{depend street3}}{{var street3}}<br />{{/depend}}
{{depend street4}}{{var street4}}<br />{{/depend}}
{{if city}}{{var city}},  {{/if}}{{if region}}{{var region}}, {{/if}}{{if postcode}}{{var postcode}}{{/if}}<br />
{{var country}}<br />
{{depend telephone}}T: <a href="tel:{{var telephone}}">{{var telephone}}</a>{{/depend}}
{{depend fax}}<br />F: {{var fax}}{{/depend}}
{{depend vat_id}}<br />VAT: {{var vat_id}}{{/depend}}

enter image description here You should save the original formatted address just in case. After adjusting it, just clear cache config.
Hope this will help you.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top