Address.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Block\Account\Dashboard;
  7. use Magento\Customer\Api\Data\AddressInterface;
  8. use Magento\Customer\Model\Address\Mapper;
  9. use Magento\Framework\Exception\NoSuchEntityException;
  10. /**
  11. * Class to manage customer dashboard addresses section
  12. *
  13. * @api
  14. * @since 100.0.2
  15. */
  16. class Address extends \Magento\Framework\View\Element\Template
  17. {
  18. /**
  19. * @var \Magento\Customer\Model\Address\Config
  20. */
  21. protected $_addressConfig;
  22. /**
  23. * @var \Magento\Customer\Helper\Session\CurrentCustomer
  24. */
  25. protected $currentCustomer;
  26. /**
  27. * @var \Magento\Customer\Helper\Session\CurrentCustomerAddress
  28. */
  29. protected $currentCustomerAddress;
  30. /**
  31. * @var Mapper
  32. */
  33. protected $addressMapper;
  34. /**
  35. * @param \Magento\Framework\View\Element\Template\Context $context
  36. * @param \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer
  37. * @param \Magento\Customer\Helper\Session\CurrentCustomerAddress $currentCustomerAddress
  38. * @param \Magento\Customer\Model\Address\Config $addressConfig
  39. * @param Mapper $addressMapper
  40. * @param array $data
  41. */
  42. public function __construct(
  43. \Magento\Framework\View\Element\Template\Context $context,
  44. \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,
  45. \Magento\Customer\Helper\Session\CurrentCustomerAddress $currentCustomerAddress,
  46. \Magento\Customer\Model\Address\Config $addressConfig,
  47. Mapper $addressMapper,
  48. array $data = []
  49. ) {
  50. $this->currentCustomer = $currentCustomer;
  51. $this->currentCustomerAddress = $currentCustomerAddress;
  52. $this->_addressConfig = $addressConfig;
  53. parent::__construct($context, $data);
  54. $this->addressMapper = $addressMapper;
  55. }
  56. /**
  57. * Get the logged in customer
  58. *
  59. * @return \Magento\Customer\Api\Data\CustomerInterface|null
  60. */
  61. public function getCustomer()
  62. {
  63. try {
  64. return $this->currentCustomer->getCustomer();
  65. } catch (NoSuchEntityException $e) {
  66. return null;
  67. }
  68. }
  69. /**
  70. * HTML for Shipping Address
  71. *
  72. * @return \Magento\Framework\Phrase|string
  73. */
  74. public function getPrimaryShippingAddressHtml()
  75. {
  76. try {
  77. $address = $this->currentCustomerAddress->getDefaultShippingAddress();
  78. } catch (NoSuchEntityException $e) {
  79. return __('You have not set a default shipping address.');
  80. }
  81. if ($address) {
  82. return $this->_getAddressHtml($address);
  83. } else {
  84. return __('You have not set a default shipping address.');
  85. }
  86. }
  87. /**
  88. * HTML for Billing Address
  89. *
  90. * @return \Magento\Framework\Phrase|string
  91. */
  92. public function getPrimaryBillingAddressHtml()
  93. {
  94. try {
  95. $address = $this->currentCustomerAddress->getDefaultBillingAddress();
  96. } catch (NoSuchEntityException $e) {
  97. return __('You have not set a default billing address.');
  98. }
  99. if ($address) {
  100. return $this->_getAddressHtml($address);
  101. } else {
  102. return __('You have not set a default billing address.');
  103. }
  104. }
  105. /**
  106. * @return string
  107. */
  108. public function getPrimaryShippingAddressEditUrl()
  109. {
  110. if (!$this->getCustomer()) {
  111. return '';
  112. } else {
  113. $address = $this->currentCustomerAddress->getDefaultShippingAddress();
  114. $addressId = $address ? $address->getId() : null;
  115. return $this->_urlBuilder->getUrl(
  116. 'customer/address/edit',
  117. ['id' => $addressId]
  118. );
  119. }
  120. }
  121. /**
  122. * @return string
  123. */
  124. public function getPrimaryBillingAddressEditUrl()
  125. {
  126. if (!$this->getCustomer()) {
  127. return '';
  128. } else {
  129. $address = $this->currentCustomerAddress->getDefaultBillingAddress();
  130. $addressId = $address ? $address->getId() : null;
  131. return $this->_urlBuilder->getUrl(
  132. 'customer/address/edit',
  133. ['id' => $addressId]
  134. );
  135. }
  136. }
  137. /**
  138. * @return string
  139. */
  140. public function getAddressBookUrl()
  141. {
  142. return $this->getUrl('customer/address/');
  143. }
  144. /**
  145. * Render an address as HTML and return the result
  146. *
  147. * @param AddressInterface $address
  148. * @return string
  149. */
  150. protected function _getAddressHtml($address)
  151. {
  152. /** @var \Magento\Customer\Block\Address\Renderer\RendererInterface $renderer */
  153. $renderer = $this->_addressConfig->getFormatByCode('html')->getRenderer();
  154. return $renderer->renderArray($this->addressMapper->toFlatArray($address));
  155. }
  156. }