All we need is an easy explanation of the problem, so here it is.
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 ๐
How to solve :
I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.
Method 1
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.
Method 2
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}}" rel="nofollow noreferrer noopener">{{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.
Note: Use and implement method 1 because this method fully tested our system.
Thank you ๐
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0