Onepage.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\Controller;
  7. use Magento\Customer\Api\AccountManagementInterface;
  8. use Magento\Customer\Api\CustomerRepositoryInterface;
  9. use Magento\Framework\Exception\NotFoundException;
  10. use Magento\Framework\App\RequestInterface;
  11. /**
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. abstract class Onepage extends Action
  15. {
  16. /**
  17. * @var array
  18. */
  19. protected $_sectionUpdateFunctions = [
  20. 'payment-method' => '_getPaymentMethodsHtml',
  21. 'shipping-method' => '_getShippingMethodsHtml',
  22. 'review' => '_getReviewHtml',
  23. ];
  24. /**
  25. * @var \Magento\Sales\Model\Order
  26. */
  27. protected $_order;
  28. /**
  29. * Core registry
  30. *
  31. * @var \Magento\Framework\Registry
  32. */
  33. protected $_coreRegistry = null;
  34. /**
  35. * @var \Magento\Framework\Translate\InlineInterface
  36. */
  37. protected $_translateInline;
  38. /**
  39. * @var \Magento\Framework\Data\Form\FormKey\Validator
  40. */
  41. protected $_formKeyValidator;
  42. /**
  43. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  44. */
  45. protected $scopeConfig;
  46. /**
  47. * @var \Magento\Framework\View\LayoutFactory
  48. */
  49. protected $layoutFactory;
  50. /**
  51. * @var \Magento\Quote\Api\CartRepositoryInterface
  52. */
  53. protected $quoteRepository;
  54. /**
  55. * @var \Magento\Framework\View\Result\PageFactory
  56. */
  57. protected $resultPageFactory;
  58. /**
  59. * @var \Magento\Framework\View\Result\LayoutFactory
  60. */
  61. protected $resultLayoutFactory;
  62. /**
  63. * @var \Magento\Framework\Controller\Result\RawFactory
  64. */
  65. protected $resultRawFactory;
  66. /**
  67. * @var \Magento\Framework\Controller\Result\JsonFactory
  68. */
  69. protected $resultJsonFactory;
  70. /**
  71. * @param \Magento\Framework\App\Action\Context $context
  72. * @param \Magento\Customer\Model\Session $customerSession
  73. * @param CustomerRepositoryInterface $customerRepository
  74. * @param AccountManagementInterface $accountManagement
  75. * @param \Magento\Framework\Registry $coreRegistry
  76. * @param \Magento\Framework\Translate\InlineInterface $translateInline
  77. * @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
  78. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  79. * @param \Magento\Framework\View\LayoutFactory $layoutFactory
  80. * @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
  81. * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
  82. * @param \Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory
  83. * @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
  84. * @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
  85. *
  86. * @codeCoverageIgnore
  87. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  88. */
  89. public function __construct(
  90. \Magento\Framework\App\Action\Context $context,
  91. \Magento\Customer\Model\Session $customerSession,
  92. CustomerRepositoryInterface $customerRepository,
  93. AccountManagementInterface $accountManagement,
  94. \Magento\Framework\Registry $coreRegistry,
  95. \Magento\Framework\Translate\InlineInterface $translateInline,
  96. \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
  97. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  98. \Magento\Framework\View\LayoutFactory $layoutFactory,
  99. \Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
  100. \Magento\Framework\View\Result\PageFactory $resultPageFactory,
  101. \Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory,
  102. \Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
  103. \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
  104. ) {
  105. $this->_coreRegistry = $coreRegistry;
  106. $this->_translateInline = $translateInline;
  107. $this->_formKeyValidator = $formKeyValidator;
  108. $this->scopeConfig = $scopeConfig;
  109. $this->layoutFactory = $layoutFactory;
  110. $this->quoteRepository = $quoteRepository;
  111. $this->resultPageFactory = $resultPageFactory;
  112. $this->resultLayoutFactory = $resultLayoutFactory;
  113. $this->resultRawFactory = $resultRawFactory;
  114. $this->resultJsonFactory = $resultJsonFactory;
  115. parent::__construct(
  116. $context,
  117. $customerSession,
  118. $customerRepository,
  119. $accountManagement
  120. );
  121. }
  122. /**
  123. * Dispatch request
  124. *
  125. * @param RequestInterface $request
  126. * @return \Magento\Framework\App\ResponseInterface
  127. * @throws \Magento\Framework\Exception\NotFoundException
  128. */
  129. public function dispatch(RequestInterface $request)
  130. {
  131. $this->_request = $request;
  132. $result = $this->_preDispatchValidateCustomer();
  133. if ($result instanceof \Magento\Framework\Controller\ResultInterface) {
  134. return $result;
  135. }
  136. /** @var \Magento\Quote\Model\Quote $quote */
  137. $quote = $this->_objectManager->get(\Magento\Checkout\Model\Session::class)->getQuote();
  138. if ($quote->isMultipleShippingAddresses()) {
  139. $quote->removeAllAddresses();
  140. }
  141. if (!$this->_canShowForUnregisteredUsers()) {
  142. throw new NotFoundException(__('Page not found.'));
  143. }
  144. return parent::dispatch($request);
  145. }
  146. /**
  147. * @return \Magento\Framework\Controller\Result\Raw
  148. */
  149. protected function _ajaxRedirectResponse()
  150. {
  151. $resultRaw = $this->resultRawFactory->create();
  152. $resultRaw->setStatusHeader(403, '1.1', 'Session Expired')
  153. ->setHeader('Login-Required', 'true');
  154. return $resultRaw;
  155. }
  156. /**
  157. * Validate ajax request and redirect on failure
  158. *
  159. * @return bool
  160. */
  161. protected function _expireAjax()
  162. {
  163. $quote = $this->getOnepage()->getQuote();
  164. if (!$quote->hasItems() || $quote->getHasError() || !$quote->validateMinimumAmount()) {
  165. return true;
  166. }
  167. $action = $this->getRequest()->getActionName();
  168. if ($this->_objectManager->get(\Magento\Checkout\Model\Session::class)->getCartWasUpdated(true)
  169. &&
  170. !in_array($action, ['index', 'progress'])
  171. ) {
  172. return true;
  173. }
  174. return false;
  175. }
  176. /**
  177. * Render HTML based on requested layout handle name
  178. *
  179. * @param string $handle
  180. * @return string
  181. */
  182. protected function _getHtmlByHandle($handle)
  183. {
  184. $layout = $this->layoutFactory->create();
  185. $layout->getUpdate()->load([$handle]);
  186. $layout->generateXml();
  187. $layout->generateElements();
  188. $output = $layout->getOutput();
  189. $this->_translateInline->processResponseBody($output);
  190. return $output;
  191. }
  192. /**
  193. * Get shipping method step html
  194. *
  195. * @return string
  196. * @codeCoverageIgnore
  197. */
  198. protected function _getShippingMethodsHtml()
  199. {
  200. return $this->_getHtmlByHandle('checkout_onepage_shippingmethod');
  201. }
  202. /**
  203. * Get payment method step html
  204. *
  205. * @return string
  206. * @codeCoverageIgnore
  207. */
  208. protected function _getPaymentMethodsHtml()
  209. {
  210. return $this->_getHtmlByHandle('checkout_onepage_paymentmethod');
  211. }
  212. /**
  213. * Get progress html checkout step
  214. *
  215. * @param string $checkoutStep
  216. * @return mixed
  217. */
  218. protected function getProgressHtml($checkoutStep = '')
  219. {
  220. $layout = $this->layoutFactory->create();
  221. $layout->getUpdate()->load(['checkout_onepage_progress']);
  222. $layout->generateXml();
  223. $layout->generateElements();
  224. $block = $layout->getBlock('progress')->setAttribute('next_step', $checkoutStep);
  225. return $block->toHtml();
  226. }
  227. /**
  228. * Get one page checkout model
  229. *
  230. * @return \Magento\Checkout\Model\Type\Onepage
  231. * @codeCoverageIgnore
  232. */
  233. public function getOnepage()
  234. {
  235. return $this->_objectManager->get(\Magento\Checkout\Model\Type\Onepage::class);
  236. }
  237. /**
  238. * Check can page show for unregistered users
  239. *
  240. * @return boolean
  241. */
  242. protected function _canShowForUnregisteredUsers()
  243. {
  244. return $this->_objectManager->get(
  245. \Magento\Customer\Model\Session::class
  246. )->isLoggedIn() || $this->getRequest()->getActionName() == 'index' || $this->_objectManager->get(
  247. \Magento\Checkout\Helper\Data::class
  248. )->isAllowedGuestCheckout(
  249. $this->getOnepage()->getQuote()
  250. ) || !$this->_objectManager->get(
  251. \Magento\Checkout\Helper\Data::class
  252. )->isCustomerMustBeLogged();
  253. }
  254. }