How to get Customer Address in formated way and print it - Magento2.3
-
13-04-2021 - |
La 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.
Autres conseils
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}}
You should save the original formatted address just in case.
After adjusting it, just clear cache config.
Hope this will help you.