Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Onepage.php
Go to the documentation of this file.
1 <?php
7 
12 
16 abstract class Onepage extends Action
17 {
22  'payment-method' => '_getPaymentMethodsHtml',
23  'shipping-method' => '_getShippingMethodsHtml',
24  'review' => '_getReviewHtml',
25  ];
26 
30  protected $_order;
31 
37  protected $_coreRegistry = null;
38 
42  protected $_translateInline;
43 
47  protected $_formKeyValidator;
48 
52  protected $scopeConfig;
53 
57  protected $layoutFactory;
58 
62  protected $quoteRepository;
63 
67  protected $resultPageFactory;
68 
73 
77  protected $resultRawFactory;
78 
82  protected $resultJsonFactory;
83 
103  public function __construct(
104  \Magento\Framework\App\Action\Context $context,
105  \Magento\Customer\Model\Session $customerSession,
108  \Magento\Framework\Registry $coreRegistry,
109  \Magento\Framework\Translate\InlineInterface $translateInline,
110  \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
111  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
112  \Magento\Framework\View\LayoutFactory $layoutFactory,
114  \Magento\Framework\View\Result\PageFactory $resultPageFactory,
115  \Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory,
116  \Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
117  \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
118  ) {
119  $this->_coreRegistry = $coreRegistry;
120  $this->_translateInline = $translateInline;
121  $this->_formKeyValidator = $formKeyValidator;
122  $this->scopeConfig = $scopeConfig;
123  $this->layoutFactory = $layoutFactory;
124  $this->quoteRepository = $quoteRepository;
125  $this->resultPageFactory = $resultPageFactory;
126  $this->resultLayoutFactory = $resultLayoutFactory;
127  $this->resultRawFactory = $resultRawFactory;
128  $this->resultJsonFactory = $resultJsonFactory;
129  parent::__construct(
130  $context,
131  $customerSession,
134  );
135  }
136 
144  public function dispatch(RequestInterface $request)
145  {
146  $this->_request = $request;
148  if ($result instanceof \Magento\Framework\Controller\ResultInterface) {
149  return $result;
150  }
151 
153  $quote = $this->_objectManager->get(\Magento\Checkout\Model\Session::class)->getQuote();
154  if ($quote->isMultipleShippingAddresses()) {
155  $quote->removeAllAddresses();
156  }
157 
158  if (!$this->_canShowForUnregisteredUsers()) {
159  throw new NotFoundException(__('Page not found.'));
160  }
161  return parent::dispatch($request);
162  }
163 
167  protected function _ajaxRedirectResponse()
168  {
169  $resultRaw = $this->resultRawFactory->create();
170  $resultRaw->setStatusHeader(403, '1.1', 'Session Expired')
171  ->setHeader('Login-Required', 'true');
172  return $resultRaw;
173  }
174 
180  protected function _expireAjax()
181  {
182  $quote = $this->getOnepage()->getQuote();
183  if (!$quote->hasItems() || $quote->getHasError() || !$quote->validateMinimumAmount()) {
184  return true;
185  }
186  $action = $this->getRequest()->getActionName();
187  if ($this->_objectManager->get(\Magento\Checkout\Model\Session::class)->getCartWasUpdated(true)
188  &&
189  !in_array($action, ['index', 'progress'])
190  ) {
191  return true;
192  }
193 
194  return false;
195  }
196 
203  protected function _getHtmlByHandle($handle)
204  {
205  $layout = $this->layoutFactory->create();
206  $layout->getUpdate()->load([$handle]);
207  $layout->generateXml();
208  $layout->generateElements();
209  $output = $layout->getOutput();
210  $this->_translateInline->processResponseBody($output);
211  return $output;
212  }
213 
220  protected function _getShippingMethodsHtml()
221  {
222  return $this->_getHtmlByHandle('checkout_onepage_shippingmethod');
223  }
224 
231  protected function _getPaymentMethodsHtml()
232  {
233  return $this->_getHtmlByHandle('checkout_onepage_paymentmethod');
234  }
235 
242  protected function getProgressHtml($checkoutStep = '')
243  {
244  $layout = $this->layoutFactory->create();
245  $layout->getUpdate()->load(['checkout_onepage_progress']);
246  $layout->generateXml();
247  $layout->generateElements();
248 
249  $block = $layout->getBlock('progress')->setAttribute('next_step', $checkoutStep);
250  return $block->toHtml();
251  }
252 
259  public function getOnepage()
260  {
261  return $this->_objectManager->get(\Magento\Checkout\Model\Type\Onepage::class);
262  }
263 
269  protected function _canShowForUnregisteredUsers()
270  {
271  return $this->_objectManager->get(
272  \Magento\Customer\Model\Session::class
273  )->isLoggedIn() || $this->getRequest()->getActionName() == 'index' || $this->_objectManager->get(
274  \Magento\Checkout\Helper\Data::class
275  )->isAllowedGuestCheckout(
276  $this->getOnepage()->getQuote()
277  ) || !$this->_objectManager->get(
278  \Magento\Checkout\Helper\Data::class
279  )->isCustomerMustBeLogged();
280  }
281 }
$quote
__()
Definition: __.php:13
$block
Definition: block.php:8
__construct(\Magento\Framework\App\Action\Context $context, \Magento\Customer\Model\Session $customerSession, CustomerRepositoryInterface $customerRepository, AccountManagementInterface $accountManagement, \Magento\Framework\Registry $coreRegistry, \Magento\Framework\Translate\InlineInterface $translateInline, \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\View\LayoutFactory $layoutFactory, \Magento\Quote\Api\CartRepositoryInterface $quoteRepository, \Magento\Framework\View\Result\PageFactory $resultPageFactory, \Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory, \Magento\Framework\Controller\Result\RawFactory $resultRawFactory, \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory)
Definition: Onepage.php:103
getProgressHtml($checkoutStep='')
Definition: Onepage.php:242
dispatch(RequestInterface $request)
Definition: Action.php:92
$handle
_preDispatchValidateCustomer($redirect=true, $addErrors=true)
Definition: Action.php:60