Checkout.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Multishipping\Controller;
  7. use Magento\Customer\Api\AccountManagementInterface;
  8. use Magento\Customer\Api\CustomerRepositoryInterface;
  9. use Magento\Framework\App\RequestInterface;
  10. use Magento\Framework\Exception\StateException;
  11. /**
  12. * Multishipping checkout controller
  13. * @SuppressWarnings(PHPMD.NumberOfChildren)
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. */
  16. abstract class Checkout extends \Magento\Checkout\Controller\Action implements
  17. \Magento\Checkout\Controller\Express\RedirectLoginInterface
  18. {
  19. /**
  20. * Constructor
  21. *
  22. * @param \Magento\Framework\App\Action\Context $context
  23. * @param \Magento\Customer\Model\Session $customerSession
  24. * @param CustomerRepositoryInterface $customerRepository
  25. * @param AccountManagementInterface $accountManagement
  26. */
  27. public function __construct(
  28. \Magento\Framework\App\Action\Context $context,
  29. \Magento\Customer\Model\Session $customerSession,
  30. CustomerRepositoryInterface $customerRepository,
  31. AccountManagementInterface $accountManagement
  32. ) {
  33. parent::__construct(
  34. $context,
  35. $customerSession,
  36. $customerRepository,
  37. $accountManagement
  38. );
  39. }
  40. /**
  41. * Retrieve checkout model
  42. *
  43. * @return \Magento\Multishipping\Model\Checkout\Type\Multishipping
  44. */
  45. protected function _getCheckout()
  46. {
  47. return $this->_objectManager->get(\Magento\Multishipping\Model\Checkout\Type\Multishipping::class);
  48. }
  49. /**
  50. * Retrieve checkout state model
  51. *
  52. * @return \Magento\Multishipping\Model\Checkout\Type\Multishipping\State
  53. */
  54. protected function _getState()
  55. {
  56. return $this->_objectManager->get(\Magento\Multishipping\Model\Checkout\Type\Multishipping\State::class);
  57. }
  58. /**
  59. * Retrieve checkout url helper
  60. *
  61. * @return \Magento\Multishipping\Helper\Url
  62. */
  63. protected function _getHelper()
  64. {
  65. return $this->_objectManager->get(\Magento\Multishipping\Helper\Url::class);
  66. }
  67. /**
  68. * Retrieve checkout session
  69. *
  70. * @return \Magento\Checkout\Model\Session
  71. */
  72. protected function _getCheckoutSession()
  73. {
  74. return $this->_objectManager->get(\Magento\Checkout\Model\Session::class);
  75. }
  76. /**
  77. * Dispatch request
  78. *
  79. * @param RequestInterface $request
  80. * @return \Magento\Framework\App\ResponseInterface
  81. * @throws \Magento\Framework\Exception\NotFoundException
  82. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  83. * @SuppressWarnings(PHPMD.NPathComplexity)
  84. */
  85. public function dispatch(RequestInterface $request)
  86. {
  87. $this->_request = $request;
  88. if ($this->_actionFlag->get('', 'redirectLogin')) {
  89. return parent::dispatch($request);
  90. }
  91. $action = $request->getActionName();
  92. $checkoutSessionQuote = $this->_getCheckoutSession()->getQuote();
  93. /**
  94. * Catch index action call to set some flags before checkout/type_multishipping model initialization
  95. */
  96. if ($action == 'index') {
  97. $checkoutSessionQuote->setIsMultiShipping(true);
  98. $this->_getCheckoutSession()->setCheckoutState(\Magento\Checkout\Model\Session::CHECKOUT_STATE_BEGIN);
  99. } elseif (!$checkoutSessionQuote->getIsMultiShipping() && !in_array(
  100. $action,
  101. ['login', 'register', 'success']
  102. )
  103. ) {
  104. $this->_redirect('*/*/index');
  105. $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
  106. return parent::dispatch($request);
  107. }
  108. if (!in_array($action, ['login', 'register'])) {
  109. $customerSession = $this->_objectManager->get(\Magento\Customer\Model\Session::class);
  110. if (!$customerSession->authenticate($this->_getHelper()->getMSLoginUrl())) {
  111. $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
  112. }
  113. if (!$this->_objectManager->get(
  114. \Magento\Multishipping\Helper\Data::class
  115. )->isMultishippingCheckoutAvailable()) {
  116. $error = $this->_getCheckout()->getMinimumAmountError();
  117. $this->messageManager->addError($error);
  118. $this->getResponse()->setRedirect($this->_getHelper()->getCartUrl());
  119. $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
  120. return parent::dispatch($request);
  121. }
  122. }
  123. $result = $this->_preDispatchValidateCustomer();
  124. if ($result instanceof \Magento\Framework\Controller\ResultInterface) {
  125. return $result;
  126. }
  127. if (!$result) {
  128. return $this->getResponse();
  129. }
  130. if ($this->_getCheckoutSession()->getCartWasUpdated(true)
  131. &&
  132. !in_array($action, ['index', 'login', 'register', 'addresses', 'success'])
  133. ) {
  134. $this->getResponse()->setRedirect($this->_getHelper()->getCartUrl());
  135. $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
  136. return parent::dispatch($request);
  137. }
  138. if ($action == 'success' && $this->_getCheckout()->getCheckoutSession()->getDisplaySuccess(true)) {
  139. return parent::dispatch($request);
  140. }
  141. try {
  142. $checkout = $this->_getCheckout();
  143. } catch (StateException $e) {
  144. $this->getResponse()->setRedirect($this->_getHelper()->getMSNewShippingUrl());
  145. $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
  146. return parent::dispatch($request);
  147. }
  148. $quote = $checkout->getQuote();
  149. if (!$quote->hasItems() || $quote->getHasError() || $quote->isVirtual()) {
  150. $this->getResponse()->setRedirect($this->_getHelper()->getCartUrl());
  151. $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
  152. }
  153. return parent::dispatch($request);
  154. }
  155. /**
  156. * Validate minimum amount
  157. *
  158. * @return bool
  159. */
  160. protected function _validateMinimumAmount()
  161. {
  162. if (!$this->_getCheckout()->validateMinimumAmount()) {
  163. $error = $this->_getCheckout()->getMinimumAmountError();
  164. $this->messageManager->addError($error);
  165. $this->_forward('backToAddresses');
  166. return false;
  167. }
  168. return true;
  169. }
  170. /**
  171. * Returns before_auth_url redirect parameter for customer session
  172. *
  173. * @return string
  174. */
  175. public function getCustomerBeforeAuthUrl()
  176. {
  177. return $this->_objectManager->create(
  178. \Magento\Framework\UrlInterface::class
  179. )->getUrl('*/*', ['_secure' => true]);
  180. }
  181. /**
  182. * Returns a list of action flags [flag_key] => boolean
  183. *
  184. * @return array
  185. */
  186. public function getActionFlagList()
  187. {
  188. return ['redirectLogin' => true];
  189. }
  190. /**
  191. * Returns login url parameter for redirect
  192. *
  193. * @return string
  194. */
  195. public function getLoginUrl()
  196. {
  197. return $this->_getHelper()->getMSLoginUrl();
  198. }
  199. /**
  200. * Returns action name which requires redirect
  201. *
  202. * @return string
  203. */
  204. public function getRedirectActionName()
  205. {
  206. return 'index';
  207. }
  208. }