Grid.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Customer\Block\Address;
  8. use Magento\Customer\Model\ResourceModel\Address\CollectionFactory as AddressCollectionFactory;
  9. use Magento\Directory\Model\CountryFactory;
  10. use Magento\Framework\Exception\NoSuchEntityException;
  11. /**
  12. * Customer address grid
  13. *
  14. * @api
  15. * @since 102.0.1
  16. */
  17. class Grid extends \Magento\Framework\View\Element\Template
  18. {
  19. /**
  20. * @var \Magento\Customer\Helper\Session\CurrentCustomer
  21. */
  22. private $currentCustomer;
  23. /**
  24. * @var \Magento\Customer\Model\ResourceModel\Address\CollectionFactory
  25. */
  26. private $addressCollectionFactory;
  27. /**
  28. * @var \Magento\Customer\Model\ResourceModel\Address\Collection
  29. */
  30. private $addressCollection;
  31. /**
  32. * @var CountryFactory
  33. */
  34. private $countryFactory;
  35. /**
  36. * @param \Magento\Framework\View\Element\Template\Context $context
  37. * @param \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer
  38. * @param AddressCollectionFactory $addressCollectionFactory
  39. * @param CountryFactory $countryFactory
  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. AddressCollectionFactory $addressCollectionFactory,
  46. CountryFactory $countryFactory,
  47. array $data = []
  48. ) {
  49. $this->currentCustomer = $currentCustomer;
  50. $this->addressCollectionFactory = $addressCollectionFactory;
  51. $this->countryFactory = $countryFactory;
  52. parent::__construct($context, $data);
  53. }
  54. /**
  55. * Prepare the Address Book section layout
  56. *
  57. * @return void
  58. * @throws \Magento\Framework\Exception\LocalizedException
  59. * @since 102.0.1
  60. */
  61. protected function _prepareLayout(): void
  62. {
  63. parent::_prepareLayout();
  64. $this->preparePager();
  65. }
  66. /**
  67. * Generate and return "New Address" URL
  68. *
  69. * @return string
  70. * @since 102.0.1
  71. */
  72. public function getAddAddressUrl(): string
  73. {
  74. return $this->getUrl('customer/address/new', ['_secure' => true]);
  75. }
  76. /**
  77. * Generate and return "Delete" URL
  78. *
  79. * @return string
  80. * @since 102.0.1
  81. */
  82. public function getDeleteUrl(): string
  83. {
  84. return $this->getUrl('customer/address/delete');
  85. }
  86. /**
  87. * Generate and return "Edit Address" URL.
  88. *
  89. * Address ID passed in parameters
  90. *
  91. * @param int $addressId
  92. * @return string
  93. * @since 102.0.1
  94. */
  95. public function getAddressEditUrl($addressId): string
  96. {
  97. return $this->getUrl('customer/address/edit', ['_secure' => true, 'id' => $addressId]);
  98. }
  99. /**
  100. * Get current additional customer addresses
  101. *
  102. * Return array of address interfaces if customer has additional addresses and false in other cases
  103. *
  104. * @return \Magento\Customer\Api\Data\AddressInterface[]
  105. * @throws \Magento\Framework\Exception\LocalizedException
  106. * @throws NoSuchEntityException
  107. * @since 102.0.1
  108. */
  109. public function getAdditionalAddresses(): array
  110. {
  111. $additional = [];
  112. $addresses = $this->getAddressCollection();
  113. $primaryAddressIds = [$this->getDefaultBilling(), $this->getDefaultShipping()];
  114. foreach ($addresses as $address) {
  115. if (!in_array((int)$address->getId(), $primaryAddressIds, true)) {
  116. $additional[] = $address->getDataModel();
  117. }
  118. }
  119. return $additional;
  120. }
  121. /**
  122. * Get current customer
  123. *
  124. * Return stored customer or get it from session
  125. *
  126. * @return \Magento\Customer\Api\Data\CustomerInterface
  127. * @since 102.0.1
  128. */
  129. public function getCustomer(): \Magento\Customer\Api\Data\CustomerInterface
  130. {
  131. $customer = $this->getData('customer');
  132. if ($customer === null) {
  133. $customer = $this->currentCustomer->getCustomer();
  134. $this->setData('customer', $customer);
  135. }
  136. return $customer;
  137. }
  138. /**
  139. * Get one string street address from the Address DTO passed in parameters
  140. *
  141. * @param \Magento\Customer\Api\Data\AddressInterface $address
  142. * @return string
  143. * @since 102.0.1
  144. */
  145. public function getStreetAddress(\Magento\Customer\Api\Data\AddressInterface $address): string
  146. {
  147. $street = $address->getStreet();
  148. if (is_array($street)) {
  149. $street = implode(', ', $street);
  150. }
  151. return $street;
  152. }
  153. /**
  154. * Get country name by $countryCode
  155. *
  156. * Using \Magento\Directory\Model\Country to get country name by $countryCode
  157. *
  158. * @param string $countryCode
  159. * @return string
  160. * @since 102.0.1
  161. */
  162. public function getCountryByCode(string $countryCode): string
  163. {
  164. /** @var \Magento\Directory\Model\Country $country */
  165. $country = $this->countryFactory->create();
  166. return $country->loadByCode($countryCode)->getName();
  167. }
  168. /**
  169. * Get default billing address
  170. *
  171. * Return address string if address found and null if not
  172. *
  173. * @return int
  174. * @throws \Magento\Framework\Exception\LocalizedException
  175. */
  176. private function getDefaultBilling(): int
  177. {
  178. $customer = $this->getCustomer();
  179. return (int)$customer->getDefaultBilling();
  180. }
  181. /**
  182. * Get default shipping address
  183. *
  184. * Return address string if address found and null if not
  185. *
  186. * @return int
  187. * @throws \Magento\Framework\Exception\LocalizedException
  188. */
  189. private function getDefaultShipping(): int
  190. {
  191. $customer = $this->getCustomer();
  192. return (int)$customer->getDefaultShipping();
  193. }
  194. /**
  195. * Get pager layout
  196. *
  197. * @return void
  198. * @throws \Magento\Framework\Exception\LocalizedException
  199. */
  200. private function preparePager(): void
  201. {
  202. $addressCollection = $this->getAddressCollection();
  203. if (null !== $addressCollection) {
  204. $pager = $this->getLayout()->createBlock(
  205. \Magento\Theme\Block\Html\Pager::class,
  206. 'customer.addresses.pager'
  207. )->setCollection($addressCollection);
  208. $this->setChild('pager', $pager);
  209. }
  210. }
  211. /**
  212. * Get customer addresses collection.
  213. *
  214. * Filters collection by customer id
  215. *
  216. * @return \Magento\Customer\Model\ResourceModel\Address\Collection
  217. * @throws NoSuchEntityException
  218. */
  219. private function getAddressCollection(): \Magento\Customer\Model\ResourceModel\Address\Collection
  220. {
  221. if (null === $this->addressCollection) {
  222. if (null === $this->getCustomer()) {
  223. throw new NoSuchEntityException(__('Customer not logged in'));
  224. }
  225. /** @var \Magento\Customer\Model\ResourceModel\Address\Collection $collection */
  226. $collection = $this->addressCollectionFactory->create();
  227. $collection->setOrder('entity_id', 'desc')
  228. ->setCustomerFilter([$this->getCustomer()->getId()]);
  229. $this->addressCollection = $collection;
  230. }
  231. return $this->addressCollection;
  232. }
  233. }