Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Checkout.php
Go to the documentation of this file.
1 <?php
7 
12 
18 abstract class Checkout extends \Magento\Checkout\Controller\Action implements
20 {
29  public function __construct(
30  \Magento\Framework\App\Action\Context $context,
31  \Magento\Customer\Model\Session $customerSession,
34  ) {
35  parent::__construct(
36  $context,
37  $customerSession,
40  );
41  }
42 
48  protected function _getCheckout()
49  {
50  return $this->_objectManager->get(\Magento\Multishipping\Model\Checkout\Type\Multishipping::class);
51  }
52 
58  protected function _getState()
59  {
60  return $this->_objectManager->get(\Magento\Multishipping\Model\Checkout\Type\Multishipping\State::class);
61  }
62 
68  protected function _getHelper()
69  {
70  return $this->_objectManager->get(\Magento\Multishipping\Helper\Url::class);
71  }
72 
78  protected function _getCheckoutSession()
79  {
80  return $this->_objectManager->get(\Magento\Checkout\Model\Session::class);
81  }
82 
93  {
94  $this->_request = $request;
95  if ($this->_actionFlag->get('', 'redirectLogin')) {
96  return parent::dispatch($request);
97  }
98 
99  $action = $request->getActionName();
100 
101  $checkoutSessionQuote = $this->_getCheckoutSession()->getQuote();
105  if ($action == 'index') {
106  $checkoutSessionQuote->setIsMultiShipping(true);
107  $this->_getCheckoutSession()->setCheckoutState(\Magento\Checkout\Model\Session::CHECKOUT_STATE_BEGIN);
108  } elseif (!$checkoutSessionQuote->getIsMultiShipping() && !in_array(
109  $action,
110  ['login', 'register', 'success']
111  )
112  ) {
113  $this->_redirect('*/*/index');
114  $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
115  return parent::dispatch($request);
116  }
117 
118  if (!in_array($action, ['login', 'register'])) {
119  $customerSession = $this->_objectManager->get(\Magento\Customer\Model\Session::class);
120  if (!$customerSession->authenticate($this->_getHelper()->getMSLoginUrl())) {
121  $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
122  }
123 
124  if (!$this->_objectManager->get(
125  \Magento\Multishipping\Helper\Data::class
126  )->isMultishippingCheckoutAvailable()) {
127  $error = $this->_getCheckout()->getMinimumAmountError();
128  $this->messageManager->addError($error);
129  $this->getResponse()->setRedirect($this->_getHelper()->getCartUrl());
130  $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
131  return parent::dispatch($request);
132  }
133  }
134 
136  if ($result instanceof \Magento\Framework\Controller\ResultInterface) {
137  return $result;
138  }
139 
140  if (!$result) {
141  return $this->getResponse();
142  }
143 
144  if ($this->_getCheckoutSession()->getCartWasUpdated(true)
145  &&
146  !in_array($action, ['index', 'login', 'register', 'addresses', 'success'])
147  ) {
148  $this->getResponse()->setRedirect($this->_getHelper()->getCartUrl());
149  $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
150  return parent::dispatch($request);
151  }
152 
153  if ($action == 'success' && $this->_getCheckout()->getCheckoutSession()->getDisplaySuccess(true)) {
154  return parent::dispatch($request);
155  }
156 
157  try {
158  $checkout = $this->_getCheckout();
159  } catch (StateException $e) {
160  $this->getResponse()->setRedirect($this->_getHelper()->getMSNewShippingUrl());
161  $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
162  return parent::dispatch($request);
163  }
164 
165  $quote = $checkout->getQuote();
166  if (!$quote->hasItems() || $quote->getHasError() || $quote->isVirtual()) {
167  $this->getResponse()->setRedirect($this->_getHelper()->getCartUrl());
168  $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
169  }
170 
171  return parent::dispatch($request);
172  }
173 
179  protected function _validateMinimumAmount()
180  {
181  if (!$this->_getCheckout()->validateMinimumAmount()) {
182  $error = $this->_getCheckout()->getMinimumAmountError();
183  $this->messageManager->addError($error);
184  $this->_forward('backToAddresses');
185  return false;
186  }
187  return true;
188  }
189 
195  public function getCustomerBeforeAuthUrl()
196  {
197  return $this->_objectManager->create(
198  \Magento\Framework\UrlInterface::class
199  )->getUrl('*/*', ['_secure' => true]);
200  }
201 
207  public function getActionFlagList()
208  {
209  return ['redirectLogin' => true];
210  }
211 
217  public function getLoginUrl()
218  {
219  return $this->_getHelper()->getMSLoginUrl();
220  }
221 
227  public function getRedirectActionName()
228  {
229  return 'index';
230  }
231 }
_redirect($path, $arguments=[])
Definition: Action.php:167
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$quote
__construct(\Magento\Framework\App\Action\Context $context, \Magento\Customer\Model\Session $customerSession, CustomerRepositoryInterface $customerRepository, AccountManagementInterface $accountManagement)
Definition: Checkout.php:29
_forward($action, $controller=null, $module=null, array $params=null)
Definition: Action.php:137
dispatch(RequestInterface $request)
Definition: Checkout.php:92
_preDispatchValidateCustomer($redirect=true, $addErrors=true)
Definition: Action.php:60