Results.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Multishipping\Block\Checkout;
  8. use Magento\Customer\Model\Address\Config as AddressConfig;
  9. use Magento\Framework\Exception\LocalizedException;
  10. use Magento\Framework\Session\SessionManagerInterface;
  11. use Magento\Framework\View\Element\Template\Context;
  12. use Magento\Multishipping\Model\Checkout\Type\Multishipping;
  13. use Magento\Quote\Model\Quote\Address as QuoteAddress;
  14. use Magento\Sales\Api\OrderRepositoryInterface;
  15. use Magento\Sales\Model\Order\Address as OrderAddress;
  16. use Magento\Theme\Block\Html\Title;
  17. /**
  18. * Multi-shipping checkout results information
  19. *
  20. * @api
  21. * @since 100.2.1
  22. */
  23. class Results extends Success
  24. {
  25. /**
  26. * @var AddressConfig
  27. */
  28. private $addressConfig;
  29. /**
  30. * @var OrderRepositoryInterface
  31. */
  32. private $orderRepository;
  33. /**
  34. * @var SessionManagerInterface
  35. */
  36. private $session;
  37. /**
  38. * @param Context $context
  39. * @param Multishipping $multishipping
  40. * @param AddressConfig $addressConfig
  41. * @param OrderRepositoryInterface $orderRepository
  42. * @param SessionManagerInterface $session
  43. * @param array $data
  44. */
  45. public function __construct(
  46. Context $context,
  47. Multishipping $multishipping,
  48. AddressConfig $addressConfig,
  49. OrderRepositoryInterface $orderRepository,
  50. SessionManagerInterface $session,
  51. array $data = []
  52. ) {
  53. parent::__construct($context, $multishipping, $data);
  54. $this->addressConfig = $addressConfig;
  55. $this->orderRepository = $orderRepository;
  56. $this->session = $session;
  57. }
  58. /**
  59. * Returns shipping addresses from quote.
  60. *
  61. * @return array
  62. * @since 100.2.1
  63. */
  64. public function getQuoteShippingAddresses(): array
  65. {
  66. return $this->_multishipping->getQuote()->getAllShippingAddresses();
  67. }
  68. /**
  69. * Returns all failed addresses from quote.
  70. *
  71. * @return array
  72. * @since 100.2.1
  73. */
  74. public function getFailedAddresses(): array
  75. {
  76. $addresses = $this->getQuoteShippingAddresses();
  77. if ($this->getAddressError($this->getQuoteBillingAddress())) {
  78. $addresses[] = $this->getQuoteBillingAddress();
  79. }
  80. return $addresses;
  81. }
  82. /**
  83. * Retrieve order shipping address.
  84. *
  85. * @param int $orderId
  86. * @return OrderAddress|null
  87. * @since 100.2.1
  88. */
  89. public function getOrderShippingAddress(int $orderId)
  90. {
  91. return $this->orderRepository->get($orderId)->getShippingAddress();
  92. }
  93. /**
  94. * Retrieve quote billing address.
  95. *
  96. * @return QuoteAddress
  97. * @since 100.2.1
  98. */
  99. public function getQuoteBillingAddress(): QuoteAddress
  100. {
  101. return $this->getCheckout()->getQuote()->getBillingAddress();
  102. }
  103. /**
  104. * Returns formatted shipping address from placed order.
  105. *
  106. * @param OrderAddress $address
  107. * @return string
  108. * @since 100.2.1
  109. */
  110. public function formatOrderShippingAddress(OrderAddress $address): string
  111. {
  112. return $this->getAddressOneline($address->getData());
  113. }
  114. /**
  115. * Returns formatted shipping address from quote.
  116. *
  117. * @param QuoteAddress $address
  118. * @return string
  119. * @since 100.2.1
  120. */
  121. public function formatQuoteShippingAddress(QuoteAddress $address): string
  122. {
  123. return $this->getAddressOneline($address->getData());
  124. }
  125. /**
  126. * Checks if address type is shipping.
  127. *
  128. * @param QuoteAddress $address
  129. * @return bool
  130. * @since 100.2.1
  131. */
  132. public function isShippingAddress(QuoteAddress $address): bool
  133. {
  134. return $address->getAddressType() === QuoteAddress::ADDRESS_TYPE_SHIPPING;
  135. }
  136. /**
  137. * Get unescaped address formatted as one line string.
  138. *
  139. * @param array $address
  140. * @return string
  141. */
  142. private function getAddressOneline(array $address): string
  143. {
  144. $renderer = $this->addressConfig->getFormatByCode('oneline')->getRenderer();
  145. return $renderer->renderArray($address);
  146. }
  147. /**
  148. * Returns address error.
  149. *
  150. * @param QuoteAddress $address
  151. * @return string
  152. * @since 100.2.1
  153. */
  154. public function getAddressError(QuoteAddress $address): string
  155. {
  156. $errors = $this->session->getAddressErrors();
  157. return $errors[$address->getId()] ?? '';
  158. }
  159. /**
  160. * Add title to block head.
  161. *
  162. * @throws LocalizedException
  163. * @return Success
  164. * @since 100.2.1
  165. */
  166. protected function _prepareLayout(): Success
  167. {
  168. /** @var Title $pageTitle */
  169. $pageTitle = $this->getLayout()->getBlock('page.main.title');
  170. if ($pageTitle) {
  171. $title = $this->getOrderIds() ? $pageTitle->getPartlySuccessTitle() : $pageTitle->getFailedTitle();
  172. $pageTitle->setPageTitle($title);
  173. }
  174. return parent::_prepareLayout();
  175. }
  176. }