Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Book.php
Go to the documentation of this file.
1 <?php
7 
11 
20 {
24  protected $currentCustomer;
25 
30 
34  protected $addressRepository;
35 
39  protected $_addressConfig;
40 
44  protected $addressMapper;
45 
55  public function __construct(
56  \Magento\Framework\View\Element\Template\Context $context,
59  \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,
60  \Magento\Customer\Model\Address\Config $addressConfig,
62  array $data = []
63  ) {
64  $this->customerRepository = $customerRepository;
65  $this->currentCustomer = $currentCustomer;
66  $this->addressRepository = $addressRepository;
67  $this->_addressConfig = $addressConfig;
68  $this->addressMapper = $addressMapper;
69  parent::__construct($context, $data);
70  }
71 
75  protected function _prepareLayout()
76  {
77  $this->pageConfig->getTitle()->set(__('Address Book'));
78  return parent::_prepareLayout();
79  }
80 
84  public function getAddAddressUrl()
85  {
86  return $this->getUrl('customer/address/new', ['_secure' => true]);
87  }
88 
92  public function getBackUrl()
93  {
94  if ($this->getRefererUrl()) {
95  return $this->getRefererUrl();
96  }
97  return $this->getUrl('customer/account/', ['_secure' => true]);
98  }
99 
103  public function getDeleteUrl()
104  {
105  return $this->getUrl('customer/address/delete');
106  }
107 
112  public function getAddressEditUrl($addressId)
113  {
114  return $this->getUrl('customer/address/edit', ['_secure' => true, 'id' => $addressId]);
115  }
116 
120  public function hasPrimaryAddress()
121  {
122  return $this->getDefaultBilling() || $this->getDefaultShipping();
123  }
124 
128  public function getAdditionalAddresses()
129  {
130  try {
131  $addresses = $this->customerRepository->getById($this->currentCustomer->getCustomerId())->getAddresses();
132  } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
133  return false;
134  }
135  $primaryAddressIds = [$this->getDefaultBilling(), $this->getDefaultShipping()];
136  foreach ($addresses as $address) {
137  if (!in_array($address->getId(), $primaryAddressIds)) {
138  $additional[] = $address;
139  }
140  }
141  return empty($additional) ? false : $additional;
142  }
143 
150  public function getAddressHtml(\Magento\Customer\Api\Data\AddressInterface $address = null)
151  {
152  if ($address !== null) {
154  $renderer = $this->_addressConfig->getFormatByCode('html')->getRenderer();
155  return $renderer->renderArray($this->addressMapper->toFlatArray($address));
156  }
157  return '';
158  }
159 
163  public function getCustomer()
164  {
165  $customer = $this->getData('customer');
166  if ($customer === null) {
167  try {
168  $customer = $this->customerRepository->getById($this->currentCustomer->getCustomerId());
169  } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
170  return null;
171  }
172  $this->setData('customer', $customer);
173  }
174  return $customer;
175  }
176 
180  public function getDefaultBilling()
181  {
182  $customer = $this->getCustomer();
183  if ($customer === null) {
184  return null;
185  } else {
186  return $customer->getDefaultBilling();
187  }
188  }
189 
194  public function getAddressById($addressId)
195  {
196  try {
197  return $this->addressRepository->getById($addressId);
198  } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
199  return null;
200  }
201  }
202 
206  public function getDefaultShipping()
207  {
208  $customer = $this->getCustomer();
209  if ($customer === null) {
210  return null;
211  } else {
212  return $customer->getDefaultShipping();
213  }
214  }
215 }
getData($key='', $index=null)
Definition: DataObject.php:119
$customer
Definition: customers.php:11
return false
Definition: gallery.phtml:36
$addresses
Definition: address_list.php:7
__()
Definition: __.php:13
$address
Definition: customer.php:38
__construct(\Magento\Framework\View\Element\Template\Context $context, CustomerRepositoryInterface $customerRepository, AddressRepositoryInterface $addressRepository, \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer, \Magento\Customer\Model\Address\Config $addressConfig, Mapper $addressMapper, array $data=[])
Definition: Book.php:55
setData($key, $value=null)
Definition: DataObject.php:72