Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Session.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Checkout\Model;
7 
10 use Magento\Quote\Model\QuoteIdMaskFactory;
11 
18 {
22  const CHECKOUT_STATE_BEGIN = 'begin';
23 
29  protected $_quote;
30 
36  protected $_customer;
37 
43  protected $_loadInactive = false;
44 
50  protected $_order;
51 
55  protected $_orderFactory;
56 
60  protected $_customerSession;
61 
65  protected $quoteRepository;
66 
70  protected $_remoteAddress;
71 
75  protected $_eventManager;
76 
80  protected $_storeManager;
81 
86 
91 
95  protected $isQuoteMasked;
96 
100  protected $quoteFactory;
101 
123  public function __construct(
124  \Magento\Framework\App\Request\Http $request,
125  \Magento\Framework\Session\SidResolverInterface $sidResolver,
127  \Magento\Framework\Session\SaveHandlerInterface $saveHandler,
128  \Magento\Framework\Session\ValidatorInterface $validator,
129  \Magento\Framework\Session\StorageInterface $storage,
130  \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager,
131  \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory,
132  \Magento\Framework\App\State $appState,
133  \Magento\Sales\Model\OrderFactory $orderFactory,
134  \Magento\Customer\Model\Session $customerSession,
136  \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress,
137  \Magento\Framework\Event\ManagerInterface $eventManager,
140  QuoteIdMaskFactory $quoteIdMaskFactory,
141  \Magento\Quote\Model\QuoteFactory $quoteFactory
142  ) {
143  $this->_orderFactory = $orderFactory;
144  $this->_customerSession = $customerSession;
145  $this->quoteRepository = $quoteRepository;
146  $this->_remoteAddress = $remoteAddress;
147  $this->_eventManager = $eventManager;
148  $this->_storeManager = $storeManager;
149  $this->customerRepository = $customerRepository;
150  $this->quoteIdMaskFactory = $quoteIdMaskFactory;
151  $this->quoteFactory = $quoteFactory;
152  parent::__construct(
153  $request,
154  $sidResolver,
156  $saveHandler,
157  $validator,
158  $storage,
161  $appState
162  );
163  }
164 
172  public function setCustomerData($customer)
173  {
174  $this->_customer = $customer;
175  return $this;
176  }
177 
184  public function hasQuote()
185  {
186  return isset($this->_quote);
187  }
188 
196  public function setLoadInactive($load = true)
197  {
198  $this->_loadInactive = $load;
199  return $this;
200  }
201 
209  public function getQuote()
210  {
211  $this->_eventManager->dispatch('custom_quote_process', ['checkout_session' => $this]);
212 
213  if ($this->_quote === null) {
214  $quote = $this->quoteFactory->create();
215  if ($this->getQuoteId()) {
216  try {
217  if ($this->_loadInactive) {
218  $quote = $this->quoteRepository->get($this->getQuoteId());
219  } else {
220  $quote = $this->quoteRepository->getActive($this->getQuoteId());
221  }
222 
228  if ($quote->getQuoteCurrencyCode() != $this->_storeManager->getStore()->getCurrentCurrencyCode()) {
229  $quote->setStore($this->_storeManager->getStore());
230  $this->quoteRepository->save($quote->collectTotals());
231  /*
232  * We mast to create new quote object, because collectTotals()
233  * can to create links with other objects.
234  */
235  $quote = $this->quoteRepository->get($this->getQuoteId());
236  }
237  } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
238  $this->setQuoteId(null);
239  }
240  }
241 
242  if (!$this->getQuoteId()) {
243  if ($this->_customerSession->isLoggedIn() || $this->_customer) {
244  $customerId = $this->_customer
245  ? $this->_customer->getId()
246  : $this->_customerSession->getCustomerId();
247  try {
248  $quote = $this->quoteRepository->getActiveForCustomer($customerId);
249  $this->setQuoteId($quote->getId());
250  } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
251  }
252  } else {
253  $quote->setIsCheckoutCart(true);
254  $this->_eventManager->dispatch('checkout_quote_init', ['quote' => $quote]);
255  }
256  }
257 
258  if ($this->_customer) {
259  $quote->setCustomer($this->_customer);
260  } elseif ($this->_customerSession->isLoggedIn()) {
261  $quote->setCustomer($this->customerRepository->getById($this->_customerSession->getCustomerId()));
262  }
263 
264  $quote->setStore($this->_storeManager->getStore());
265  $this->_quote = $quote;
266  }
267 
268  if (!$this->isQuoteMasked() && !$this->_customerSession->isLoggedIn() && $this->getQuoteId()) {
269  $quoteId = $this->getQuoteId();
271  $quoteIdMask = $this->quoteIdMaskFactory->create()->load($quoteId, 'quote_id');
272  if ($quoteIdMask->getMaskedId() === null) {
273  $quoteIdMask->setQuoteId($quoteId)->save();
274  }
275  $this->setIsQuoteMasked(true);
276  }
277 
278  $remoteAddress = $this->_remoteAddress->getRemoteAddress();
279  if ($remoteAddress) {
280  $this->_quote->setRemoteIp($remoteAddress);
281  $xForwardIp = $this->request->getServer('HTTP_X_FORWARDED_FOR');
282  $this->_quote->setXForwardedFor($xForwardIp);
283  }
284 
285  return $this->_quote;
286  }
287 
292  protected function _getQuoteIdKey()
293  {
294  return 'quote_id_' . $this->_storeManager->getStore()->getWebsiteId();
295  }
296 
302  public function setQuoteId($quoteId)
303  {
304  $this->storage->setData($this->_getQuoteIdKey(), $quoteId);
305  }
306 
311  public function getQuoteId()
312  {
313  return $this->getData($this->_getQuoteIdKey());
314  }
315 
321  public function loadCustomerQuote()
322  {
323  if (!$this->_customerSession->getCustomerId()) {
324  return $this;
325  }
326 
327  $this->_eventManager->dispatch('load_customer_quote_before', ['checkout_session' => $this]);
328 
329  try {
330  $customerQuote = $this->quoteRepository->getForCustomer($this->_customerSession->getCustomerId());
331  } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
332  $customerQuote = $this->quoteFactory->create();
333  }
334  $customerQuote->setStoreId($this->_storeManager->getStore()->getId());
335 
336  if ($customerQuote->getId() && $this->getQuoteId() != $customerQuote->getId()) {
337  if ($this->getQuoteId()) {
338  $this->quoteRepository->save(
339  $customerQuote->merge($this->getQuote())->collectTotals()
340  );
341  }
342 
343  $this->setQuoteId($customerQuote->getId());
344 
345  if ($this->_quote) {
346  $this->quoteRepository->delete($this->_quote);
347  }
348  $this->_quote = $customerQuote;
349  } else {
350  $this->getQuote()->getBillingAddress();
351  $this->getQuote()->getShippingAddress();
352  $this->getQuote()->setCustomer($this->_customerSession->getCustomerDataObject())
353  ->setTotalsCollectedFlag(false)
354  ->collectTotals();
355  $this->quoteRepository->save($this->getQuote());
356  }
357  return $this;
358  }
359 
366  public function setStepData($step, $data, $value = null)
367  {
368  $steps = $this->getSteps();
369  if ($value === null) {
370  if (is_array($data)) {
371  $steps[$step] = $data;
372  }
373  } else {
374  if (!isset($steps[$step])) {
375  $steps[$step] = [];
376  }
377  if (is_string($data)) {
378  $steps[$step][$data] = $value;
379  }
380  }
381  $this->setSteps($steps);
382 
383  return $this;
384  }
385 
391  public function getStepData($step = null, $data = null)
392  {
393  $steps = $this->getSteps();
394  if ($step === null) {
395  return $steps;
396  }
397  if (!isset($steps[$step])) {
398  return false;
399  }
400  if ($data === null) {
401  return $steps[$step];
402  }
403  if (!is_string($data) || !isset($steps[$step][$data])) {
404  return false;
405  }
406  return $steps[$step][$data];
407  }
408 
415  public function clearQuote()
416  {
417  $this->_eventManager->dispatch('checkout_quote_destroy', ['quote' => $this->getQuote()]);
418  $this->_quote = null;
419  $this->setQuoteId(null);
420  $this->setLastSuccessQuoteId(null);
421  return $this;
422  }
423 
429  public function clearStorage()
430  {
431  parent::clearStorage();
432  $this->_quote = null;
433  return $this;
434  }
435 
441  public function clearHelperData()
442  {
443  $this->setRedirectUrl(null)->setLastOrderId(null)->setLastRealOrderId(null)->setAdditionalMessages(null);
444  }
445 
450  public function resetCheckout()
451  {
452  $this->setCheckoutState(self::CHECKOUT_STATE_BEGIN);
453  return $this;
454  }
455 
460  public function replaceQuote($quote)
461  {
462  $this->_quote = $quote;
463  $this->setQuoteId($quote->getId());
464  return $this;
465  }
466 
472  public function getLastRealOrder()
473  {
474  $orderId = $this->getLastRealOrderId();
475  if ($this->_order !== null && $orderId == $this->_order->getIncrementId()) {
476  return $this->_order;
477  }
478  $this->_order = $this->_orderFactory->create();
479  if ($orderId) {
480  $this->_order->loadByIncrementId($orderId);
481  }
482  return $this->_order;
483  }
484 
490  public function restoreQuote()
491  {
493  $order = $this->getLastRealOrder();
494  if ($order->getId()) {
495  try {
496  $quote = $this->quoteRepository->get($order->getQuoteId());
497  $quote->setIsActive(1)->setReservedOrderId(null);
498  $this->quoteRepository->save($quote);
499  $this->replaceQuote($quote)->unsLastRealOrderId();
500  $this->_eventManager->dispatch('restore_quote', ['order' => $order, 'quote' => $quote]);
501  return true;
502  } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
503  }
504  }
505  return false;
506  }
507 
513  protected function setIsQuoteMasked($isQuoteMasked)
514  {
515  $this->isQuoteMasked = $isQuoteMasked;
516  }
517 
522  protected function isQuoteMasked()
523  {
524  return $this->isQuoteMasked;
525  }
526 }
getStepData($step=null, $data=null)
Definition: Session.php:391
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$customer
Definition: customers.php:11
$quote
$order
Definition: order.php:55
$storeManager
setIsQuoteMasked($isQuoteMasked)
Definition: Session.php:513
setStepData($step, $data, $value=null)
Definition: Session.php:366
$value
Definition: gender.phtml:16
__construct(\Magento\Framework\App\Request\Http $request, \Magento\Framework\Session\SidResolverInterface $sidResolver, \Magento\Framework\Session\Config\ConfigInterface $sessionConfig, \Magento\Framework\Session\SaveHandlerInterface $saveHandler, \Magento\Framework\Session\ValidatorInterface $validator, \Magento\Framework\Session\StorageInterface $storage, \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager, \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory, \Magento\Framework\App\State $appState, \Magento\Sales\Model\OrderFactory $orderFactory, \Magento\Customer\Model\Session $customerSession, \Magento\Quote\Api\CartRepositoryInterface $quoteRepository, \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository, QuoteIdMaskFactory $quoteIdMaskFactory, \Magento\Quote\Model\QuoteFactory $quoteFactory)
Definition: Session.php:123