Addresses.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Multishipping\Block\Checkout;
  7. use Magento\Customer\Model\Address\Config as AddressConfig;
  8. /**
  9. * Class Addresses
  10. * Multishipping checkout choose item addresses block
  11. *
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class Addresses extends \Magento\Sales\Block\Items\AbstractItems
  16. {
  17. /**
  18. * @var \Magento\Framework\Filter\DataObject\GridFactory
  19. */
  20. protected $_filterGridFactory;
  21. /**
  22. * @var \Magento\Multishipping\Model\Checkout\Type\Multishipping
  23. */
  24. protected $_multishipping;
  25. /**
  26. * @var \Magento\Customer\Api\CustomerRepositoryInterface
  27. */
  28. protected $customerRepository;
  29. /**
  30. * @var \Magento\Customer\Model\Address\Config
  31. */
  32. private $_addressConfig;
  33. /**
  34. * @var \Magento\Customer\Model\Address\Mapper
  35. */
  36. protected $addressMapper;
  37. /**
  38. * Constructor
  39. *
  40. * @param \Magento\Framework\View\Element\Template\Context $context
  41. * @param \Magento\Framework\Filter\DataObject\GridFactory $filterGridFactory
  42. * @param \Magento\Multishipping\Model\Checkout\Type\Multishipping $multishipping
  43. * @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
  44. * @param AddressConfig $addressConfig
  45. * @param \Magento\Customer\Model\Address\Mapper $addressMapper
  46. * @param array $data
  47. */
  48. public function __construct(
  49. \Magento\Framework\View\Element\Template\Context $context,
  50. \Magento\Framework\Filter\DataObject\GridFactory $filterGridFactory,
  51. \Magento\Multishipping\Model\Checkout\Type\Multishipping $multishipping,
  52. \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
  53. AddressConfig $addressConfig,
  54. \Magento\Customer\Model\Address\Mapper $addressMapper,
  55. array $data = []
  56. ) {
  57. $this->_filterGridFactory = $filterGridFactory;
  58. $this->_multishipping = $multishipping;
  59. $this->customerRepository = $customerRepository;
  60. $this->_addressConfig = $addressConfig;
  61. parent::__construct($context, $data);
  62. $this->addressMapper = $addressMapper;
  63. $this->_isScopePrivate = true;
  64. }
  65. /**
  66. * Retrieve multishipping checkout model
  67. *
  68. * @return \Magento\Multishipping\Model\Checkout\Type\Multishipping
  69. */
  70. public function getCheckout()
  71. {
  72. return $this->_multishipping;
  73. }
  74. /**
  75. * @return $this
  76. */
  77. protected function _prepareLayout()
  78. {
  79. $this->pageConfig->getTitle()->set(
  80. __('Ship to Multiple Addresses') . ' - ' . $this->pageConfig->getTitle()->getDefault()
  81. );
  82. return parent::_prepareLayout();
  83. }
  84. /**
  85. * @return array
  86. */
  87. public function getItems()
  88. {
  89. $items = $this->getCheckout()->getQuoteShippingAddressesItems();
  90. /** @var \Magento\Framework\Filter\DataObject\Grid $itemsFilter */
  91. $itemsFilter = $this->_filterGridFactory->create();
  92. $itemsFilter->addFilter(new \Magento\Framework\Filter\Sprintf('%d'), 'qty');
  93. return $itemsFilter->filter($items);
  94. }
  95. /**
  96. * Retrieve HTML for addresses dropdown
  97. *
  98. * @param mixed $item
  99. * @param int $index
  100. * @return string
  101. */
  102. public function getAddressesHtmlSelect($item, $index)
  103. {
  104. $select = $this->getLayout()->createBlock(\Magento\Framework\View\Element\Html\Select::class)
  105. ->setName('ship[' . $index . '][' . $item->getQuoteItemId() . '][address]')
  106. ->setId('ship_' . $index . '_' . $item->getQuoteItemId() . '_address')
  107. ->setValue($item->getCustomerAddressId())
  108. ->setOptions($this->getAddressOptions());
  109. return $select->getHtml();
  110. }
  111. /**
  112. * Retrieve options for addresses dropdown
  113. *
  114. * @return array
  115. */
  116. public function getAddressOptions()
  117. {
  118. $options = $this->getData('address_options');
  119. if ($options === null) {
  120. $options = [];
  121. $addresses = [];
  122. try {
  123. $addresses = $this->customerRepository->getById($this->getCustomerId())->getAddresses();
  124. } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
  125. /** Customer does not exist */
  126. }
  127. /** @var \Magento\Customer\Api\Data\AddressInterface $address */
  128. foreach ($addresses as $address) {
  129. $label = $this->_addressConfig
  130. ->getFormatByCode(AddressConfig::DEFAULT_ADDRESS_FORMAT)
  131. ->getRenderer()
  132. ->renderArray($this->addressMapper->toFlatArray($address));
  133. $options[] = [
  134. 'value' => $address->getId(),
  135. 'label' => $label,
  136. ];
  137. }
  138. $this->setData('address_options', $options);
  139. }
  140. return $options;
  141. }
  142. /**
  143. * Retrieve active customer ID
  144. *
  145. * @return int|null
  146. */
  147. public function getCustomerId()
  148. {
  149. return $this->getCheckout()->getCustomerSession()->getCustomerId();
  150. }
  151. /**
  152. * @param mixed $item
  153. * @return string
  154. */
  155. public function getItemUrl($item)
  156. {
  157. return $this->getUrl('catalog/product/view/id/' . $item->getProductId());
  158. }
  159. /**
  160. * @param mixed $item
  161. * @return string
  162. */
  163. public function getItemDeleteUrl($item)
  164. {
  165. return $this->getUrl('*/*/removeItem', ['address' => $item->getQuoteAddressId(), 'id' => $item->getId()]);
  166. }
  167. /**
  168. * @return string
  169. */
  170. public function getPostActionUrl()
  171. {
  172. return $this->getUrl('*/*/addressesPost');
  173. }
  174. /**
  175. * @return string
  176. */
  177. public function getNewAddressUrl()
  178. {
  179. return $this->getUrl('*/checkout_address/newShipping');
  180. }
  181. /**
  182. * @return string
  183. */
  184. public function getBackUrl()
  185. {
  186. return $this->getUrl('checkout/cart/');
  187. }
  188. /**
  189. * @return bool
  190. */
  191. public function isContinueDisabled()
  192. {
  193. return !$this->getCheckout()->validateMinimumAmount();
  194. }
  195. }