Book.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Block\Address;
  7. use Magento\Customer\Api\AddressRepositoryInterface;
  8. use Magento\Customer\Model\Address\Mapper;
  9. use Magento\Customer\Block\Address\Grid as AddressesGrid;
  10. /**
  11. * Customer address book block
  12. *
  13. * @api
  14. * @author Magento Core Team <core@magentocommerce.com>
  15. * @since 100.0.2
  16. */
  17. class Book extends \Magento\Framework\View\Element\Template
  18. {
  19. /**
  20. * @var \Magento\Customer\Helper\Session\CurrentCustomer
  21. */
  22. protected $currentCustomer;
  23. /**
  24. * @var \Magento\Customer\Api\CustomerRepositoryInterface
  25. */
  26. protected $customerRepository;
  27. /**
  28. * @var AddressRepositoryInterface
  29. */
  30. protected $addressRepository;
  31. /**
  32. * @var \Magento\Customer\Model\Address\Config
  33. */
  34. protected $_addressConfig;
  35. /**
  36. * @var Mapper
  37. */
  38. protected $addressMapper;
  39. /**
  40. * @var AddressesGrid
  41. */
  42. private $addressesGrid;
  43. /**
  44. * @param \Magento\Framework\View\Element\Template\Context $context
  45. * @param CustomerRepositoryInterface|null $customerRepository
  46. * @param AddressRepositoryInterface $addressRepository
  47. * @param \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer
  48. * @param \Magento\Customer\Model\Address\Config $addressConfig
  49. * @param Mapper $addressMapper
  50. * @param array $data
  51. * @param AddressesGrid|null $addressesGrid
  52. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  53. */
  54. public function __construct(
  55. \Magento\Framework\View\Element\Template\Context $context,
  56. \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository = null,
  57. AddressRepositoryInterface $addressRepository,
  58. \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,
  59. \Magento\Customer\Model\Address\Config $addressConfig,
  60. Mapper $addressMapper,
  61. array $data = [],
  62. Grid $addressesGrid = null
  63. ) {
  64. $this->currentCustomer = $currentCustomer;
  65. $this->addressRepository = $addressRepository;
  66. $this->_addressConfig = $addressConfig;
  67. $this->addressMapper = $addressMapper;
  68. $this->addressesGrid = $addressesGrid ?: \Magento\Framework\App\ObjectManager::getInstance()
  69. ->get(AddressesGrid::class);
  70. parent::__construct($context, $data);
  71. }
  72. /**
  73. * Prepare the Address Book section layout
  74. *
  75. * @return $this
  76. */
  77. protected function _prepareLayout()
  78. {
  79. $this->pageConfig->getTitle()->set(__('Address Book'));
  80. return parent::_prepareLayout();
  81. }
  82. /**
  83. * Generate and return "New Address" URL
  84. *
  85. * @return string
  86. * @deprecated 102.0.1 not used in this block
  87. * @see \Magento\Customer\Block\Address\Grid::getAddAddressUrl
  88. */
  89. public function getAddAddressUrl()
  90. {
  91. return $this->addressesGrid->getAddAddressUrl();
  92. }
  93. /**
  94. * Generate and return "Back" URL
  95. *
  96. * @return string
  97. */
  98. public function getBackUrl()
  99. {
  100. if ($this->getRefererUrl()) {
  101. return $this->getRefererUrl();
  102. }
  103. return $this->getUrl('customer/account/', ['_secure' => true]);
  104. }
  105. /**
  106. * Generate and return "Delete" URL
  107. *
  108. * @return string
  109. * @deprecated 102.0.1 not used in this block
  110. * @see \Magento\Customer\Block\Address\Grid::getDeleteUrl
  111. */
  112. public function getDeleteUrl()
  113. {
  114. return $this->addressesGrid->getDeleteUrl();
  115. }
  116. /**
  117. * Generate and return "Edit Address" URL.
  118. *
  119. * Address ID passed in parameters
  120. *
  121. * @param int $addressId
  122. * @return string
  123. * @deprecated 102.0.1 not used in this block
  124. * @see \Magento\Customer\Block\Address\Grid::getAddressEditUrl
  125. */
  126. public function getAddressEditUrl($addressId)
  127. {
  128. return $this->addressesGrid->getAddressEditUrl($addressId);
  129. }
  130. /**
  131. * Determines is the address primary (billing or shipping)
  132. *
  133. * @return bool
  134. * @throws \Magento\Framework\Exception\LocalizedException
  135. */
  136. public function hasPrimaryAddress()
  137. {
  138. return $this->getDefaultBilling() || $this->getDefaultShipping();
  139. }
  140. /**
  141. * Get current additional customer addresses
  142. *
  143. * Will return array of address interfaces if customer have additional addresses and false in other case.
  144. *
  145. * @return \Magento\Customer\Api\Data\AddressInterface[]|bool
  146. * @throws \Magento\Framework\Exception\LocalizedException
  147. * @deprecated 102.0.1 not used in this block
  148. * @see \Magento\Customer\Block\Address\Grid::getAdditionalAddresses
  149. */
  150. public function getAdditionalAddresses()
  151. {
  152. try {
  153. $addresses = $this->addressesGrid->getAdditionalAddresses();
  154. } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
  155. }
  156. return empty($addresses) ? false : $addresses;
  157. }
  158. /**
  159. * Render an address as HTML and return the result
  160. *
  161. * @param \Magento\Customer\Api\Data\AddressInterface $address
  162. * @return string
  163. */
  164. public function getAddressHtml(\Magento\Customer\Api\Data\AddressInterface $address = null)
  165. {
  166. if ($address !== null) {
  167. /** @var \Magento\Customer\Block\Address\Renderer\RendererInterface $renderer */
  168. $renderer = $this->_addressConfig->getFormatByCode('html')->getRenderer();
  169. return $renderer->renderArray($this->addressMapper->toFlatArray($address));
  170. }
  171. return '';
  172. }
  173. /**
  174. * Get current customer
  175. *
  176. * @return \Magento\Customer\Api\Data\CustomerInterface|null
  177. */
  178. public function getCustomer()
  179. {
  180. $customer = null;
  181. try {
  182. $customer = $this->currentCustomer->getCustomer();
  183. } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
  184. }
  185. return $customer;
  186. }
  187. /**
  188. * Get customer's default billing address
  189. *
  190. * @return int|null
  191. */
  192. public function getDefaultBilling()
  193. {
  194. $customer = $this->getCustomer();
  195. if ($customer === null) {
  196. return null;
  197. } else {
  198. return $customer->getDefaultBilling();
  199. }
  200. }
  201. /**
  202. * Get customer address by ID
  203. *
  204. * @param int $addressId
  205. * @return \Magento\Customer\Api\Data\AddressInterface|null
  206. * @throws \Magento\Framework\Exception\LocalizedException
  207. */
  208. public function getAddressById($addressId)
  209. {
  210. try {
  211. return $this->addressRepository->getById($addressId);
  212. } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
  213. return null;
  214. }
  215. }
  216. /**
  217. * Get customer's default shipping address
  218. *
  219. * @return int|null
  220. */
  221. public function getDefaultShipping()
  222. {
  223. $customer = $this->getCustomer();
  224. if ($customer === null) {
  225. return null;
  226. } else {
  227. return $customer->getDefaultShipping();
  228. }
  229. }
  230. }