Select.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Multishipping\Block\Checkout\Address;
  7. use Magento\Framework\Exception\NoSuchEntityException;
  8. use Magento\Customer\Helper\Address as CustomerAddressHelper;
  9. use Magento\Customer\Api\AddressRepositoryInterface;
  10. /**
  11. * Class Select
  12. * Multishipping checkout select billing address
  13. *
  14. * @api
  15. * @since 100.0.2
  16. */
  17. class Select extends \Magento\Multishipping\Block\Checkout\AbstractMultishipping
  18. {
  19. /**
  20. * @var CustomerAddressHelper
  21. */
  22. protected $_customerAddressHelper;
  23. /**
  24. * @var \Magento\Customer\Model\Address\Mapper
  25. */
  26. protected $addressMapper;
  27. /**
  28. * @var bool
  29. */
  30. protected $_isScopePrivate = true;
  31. /**
  32. * @var \Magento\Framework\Api\FilterBuilder
  33. */
  34. protected $filterBuilder;
  35. /**
  36. * @var \Magento\Framework\Api\SearchCriteriaBuilder
  37. */
  38. protected $searchCriteriaBuilder;
  39. /**
  40. * @var AddressRepositoryInterface
  41. */
  42. protected $addressRepository;
  43. /**
  44. * Initialize dependencies.
  45. *
  46. * @param \Magento\Framework\View\Element\Template\Context $context
  47. * @param \Magento\Multishipping\Model\Checkout\Type\Multishipping $multishipping
  48. * @param CustomerAddressHelper $customerAddressHelper
  49. * @param \Magento\Customer\Model\Address\Mapper $addressMapper
  50. * @param AddressRepositoryInterface $addressRepository
  51. * @param \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
  52. * @param \Magento\Framework\Api\FilterBuilder $filterBuilder
  53. * @param array $data
  54. */
  55. public function __construct(
  56. \Magento\Framework\View\Element\Template\Context $context,
  57. \Magento\Multishipping\Model\Checkout\Type\Multishipping $multishipping,
  58. CustomerAddressHelper $customerAddressHelper,
  59. \Magento\Customer\Model\Address\Mapper $addressMapper,
  60. AddressRepositoryInterface $addressRepository,
  61. \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
  62. \Magento\Framework\Api\FilterBuilder $filterBuilder,
  63. array $data = []
  64. ) {
  65. $this->_customerAddressHelper = $customerAddressHelper;
  66. $this->addressMapper = $addressMapper;
  67. $this->addressRepository = $addressRepository;
  68. $this->searchCriteriaBuilder = $searchCriteriaBuilder;
  69. $this->filterBuilder = $filterBuilder;
  70. parent::__construct($context, $multishipping, $data);
  71. }
  72. /**
  73. * @return $this
  74. */
  75. protected function _prepareLayout()
  76. {
  77. $this->pageConfig->getTitle()->set(
  78. __('Change Billing Address') . ' - ' . $this->pageConfig->getTitle()->getDefault()
  79. );
  80. return parent::_prepareLayout();
  81. }
  82. /**
  83. * Get a list of current customer addresses.
  84. *
  85. * @return \Magento\Customer\Api\Data\AddressInterface[]
  86. */
  87. public function getAddress()
  88. {
  89. $addresses = $this->getData('address_collection');
  90. if ($addresses === null) {
  91. try {
  92. $filter = $this->filterBuilder->setField('parent_id')
  93. ->setValue($this->_multishipping->getCustomer()->getId())
  94. ->setConditionType('eq')
  95. ->create();
  96. $addresses = (array)($this->addressRepository->getList(
  97. $this->searchCriteriaBuilder->addFilters([$filter])->create()
  98. )->getItems());
  99. } catch (NoSuchEntityException $e) {
  100. return [];
  101. }
  102. $this->setData('address_collection', $addresses);
  103. }
  104. return $addresses;
  105. }
  106. /**
  107. * Represent customer address in HTML format.
  108. *
  109. * @param \Magento\Customer\Api\Data\AddressInterface $address
  110. * @return string
  111. */
  112. public function getAddressAsHtml(\Magento\Customer\Api\Data\AddressInterface $address)
  113. {
  114. $formatTypeRenderer = $this->_customerAddressHelper->getFormatTypeRenderer('html');
  115. $result = '';
  116. if ($formatTypeRenderer) {
  117. $result = $formatTypeRenderer->renderArray($this->addressMapper->toFlatArray($address));
  118. }
  119. return $result;
  120. }
  121. /**
  122. * Check if provided address is default customer billing address.
  123. *
  124. * @param \Magento\Customer\Api\Data\AddressInterface $address
  125. * @return bool
  126. */
  127. public function isAddressDefaultBilling(\Magento\Customer\Api\Data\AddressInterface $address)
  128. {
  129. return $address->getId() == $this->_multishipping->getCustomer()->getDefaultBilling();
  130. }
  131. /**
  132. * Check if provided address is default customer shipping address.
  133. *
  134. * @param \Magento\Customer\Api\Data\AddressInterface $address
  135. * @return bool
  136. */
  137. public function isAddressDefaultShipping(\Magento\Customer\Api\Data\AddressInterface $address)
  138. {
  139. return $address->getId() == $this->_multishipping->getCustomer()->getDefaultShipping();
  140. }
  141. /**
  142. * Get URL of customer address edit page.
  143. *
  144. * @param \Magento\Customer\Api\Data\AddressInterface $address
  145. * @return string
  146. */
  147. public function getEditAddressUrl(\Magento\Customer\Api\Data\AddressInterface $address)
  148. {
  149. return $this->getUrl('*/*/editAddress', ['id' => $address->getId()]);
  150. }
  151. /**
  152. * Get URL of page, at which customer billing address can be set.
  153. *
  154. * @param \Magento\Customer\Api\Data\AddressInterface $address
  155. * @return string
  156. */
  157. public function getSetAddressUrl(\Magento\Customer\Api\Data\AddressInterface $address)
  158. {
  159. return $this->getUrl('*/*/setBilling', ['id' => $address->getId()]);
  160. }
  161. /**
  162. * @return string
  163. */
  164. public function getAddNewUrl()
  165. {
  166. return $this->getUrl('*/*/newBilling');
  167. }
  168. /**
  169. * @return string
  170. */
  171. public function getBackUrl()
  172. {
  173. return $this->getUrl('*/checkout/billing');
  174. }
  175. }