Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Address.php
Go to the documentation of this file.
1 <?php
7 
11 
19 {
23  protected $_addressConfig;
24 
28  protected $currentCustomer;
29 
34 
38  protected $addressMapper;
39 
48  public function __construct(
49  \Magento\Framework\View\Element\Template\Context $context,
50  \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,
51  \Magento\Customer\Helper\Session\CurrentCustomerAddress $currentCustomerAddress,
52  \Magento\Customer\Model\Address\Config $addressConfig,
54  array $data = []
55  ) {
56  $this->currentCustomer = $currentCustomer;
57  $this->currentCustomerAddress = $currentCustomerAddress;
58  $this->_addressConfig = $addressConfig;
59  parent::__construct($context, $data);
60  $this->addressMapper = $addressMapper;
61  }
62 
68  public function getCustomer()
69  {
70  try {
71  return $this->currentCustomer->getCustomer();
72  } catch (NoSuchEntityException $e) {
73  return null;
74  }
75  }
76 
83  {
84  try {
85  $address = $this->currentCustomerAddress->getDefaultShippingAddress();
86  } catch (NoSuchEntityException $e) {
87  return __('You have not set a default shipping address.');
88  }
89 
90  if ($address) {
91  return $this->_getAddressHtml($address);
92  } else {
93  return __('You have not set a default shipping address.');
94  }
95  }
96 
103  {
104  try {
105  $address = $this->currentCustomerAddress->getDefaultBillingAddress();
106  } catch (NoSuchEntityException $e) {
107  return __('You have not set a default billing address.');
108  }
109 
110  if ($address) {
111  return $this->_getAddressHtml($address);
112  } else {
113  return __('You have not set a default billing address.');
114  }
115  }
116 
121  {
122  if (!$this->getCustomer()) {
123  return '';
124  } else {
125  $address = $this->currentCustomerAddress->getDefaultShippingAddress();
126  $addressId = $address ? $address->getId() : null;
127  return $this->_urlBuilder->getUrl(
128  'customer/address/edit',
129  ['id' => $addressId]
130  );
131  }
132  }
133 
138  {
139  if (!$this->getCustomer()) {
140  return '';
141  } else {
142  $address = $this->currentCustomerAddress->getDefaultBillingAddress();
143  $addressId = $address ? $address->getId() : null;
144  return $this->_urlBuilder->getUrl(
145  'customer/address/edit',
146  ['id' => $addressId]
147  );
148  }
149  }
150 
154  public function getAddressBookUrl()
155  {
156  return $this->getUrl('customer/address/');
157  }
158 
165  protected function _getAddressHtml($address)
166  {
168  $renderer = $this->_addressConfig->getFormatByCode('html')->getRenderer();
169  return $renderer->renderArray($this->addressMapper->toFlatArray($address));
170  }
171 }
__()
Definition: __.php:13
$address
Definition: customer.php:38
__construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer, \Magento\Customer\Helper\Session\CurrentCustomerAddress $currentCustomerAddress, \Magento\Customer\Model\Address\Config $addressConfig, Mapper $addressMapper, array $data=[])
Definition: Address.php:48