Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Session Class Reference
Inheritance diagram for Session:
SessionManager SessionManagerInterface

Public Member Functions

 __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)
 
 setCustomerData ($customer)
 
 hasQuote ()
 
 setLoadInactive ($load=true)
 
 setQuoteId ($quoteId)
 
 getQuoteId ()
 
 loadCustomerQuote ()
 
 setStepData ($step, $data, $value=null)
 
 getStepData ($step=null, $data=null)
 
 clearQuote ()
 
 clearStorage ()
 
 clearHelperData ()
 
 resetCheckout ()
 
 replaceQuote ($quote)
 
 getLastRealOrder ()
 
- Public Member Functions inherited from SessionManager
 __construct (\Magento\Framework\App\Request\Http $request, SidResolverInterface $sidResolver, ConfigInterface $sessionConfig, SaveHandlerInterface $saveHandler, ValidatorInterface $validator, StorageInterface $storage, \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager, \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory, \Magento\Framework\App\State $appState)
 
 writeClose ()
 
 __call ($method, $args)
 
 start ()
 
 isSessionExists ()
 
 getData ($key='', $clear=false)
 
 getSessionId ()
 
 getName ()
 
 setName ($name)
 
 destroy (array $options=null)
 
 clearStorage ()
 
 getCookieDomain ()
 
 getCookiePath ()
 
 getCookieLifetime ()
 
 setSessionId ($sessionId)
 
 getSessionIdForHost ($urlHost)
 
 isValidForHost ($host)
 
 isValidForPath ($path)
 
 regenerateId ()
 
 expireSessionCookie ()
 

Data Fields

const CHECKOUT_STATE_BEGIN = 'begin'
 
- Data Fields inherited from SessionManagerInterface
const HOST_KEY = '_session_hosts'
 

Protected Member Functions

 _getQuoteIdKey ()
 
 setIsQuoteMasked ($isQuoteMasked)
 
 isQuoteMasked ()
 
- Protected Member Functions inherited from SessionManager
 registerSaveHandler ()
 
 _addHost ()
 
 _getHosts ()
 
 _cleanHosts ()
 
 clearSubDomainSessionCookie ()
 

Protected Attributes

 $_quote
 
 $_customer
 
 $_loadInactive = false
 
 $_order
 
 $_orderFactory
 
 $_customerSession
 
 $quoteRepository
 
 $_remoteAddress
 
 $_eventManager
 
 $_storeManager
 
 $customerRepository
 
 $quoteIdMaskFactory
 
 $isQuoteMasked
 
 $quoteFactory
 
- Protected Attributes inherited from SessionManager
 $defaultDestroyOptions = ['send_expire_cookie' => true, 'clear_storage' => true]
 
 $validator
 
 $request
 
 $sidResolver
 
 $sessionConfig
 
 $saveHandler
 
 $storage
 
 $cookieManager
 
 $cookieMetadataFactory
 

Additional Inherited Members

- Static Protected Attributes inherited from SessionManager
static $urlHostCache = []
 

Detailed Description

@api @SuppressWarnings(PHPMD.CouplingBetweenObjects)

Since
100.0.2

Definition at line 17 of file Session.php.

Constructor & Destructor Documentation

◆ __construct()

__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 
)
Parameters
\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@SuppressWarnings(PHPMD.ExcessiveParameterList)

Definition at line 123 of file Session.php.

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  }
$storeManager

Member Function Documentation

◆ _getQuoteIdKey()

_getQuoteIdKey ( )
protected
Returns
string @codeCoverageIgnore

Definition at line 292 of file Session.php.

293  {
294  return 'quote_id_' . $this->_storeManager->getStore()->getWebsiteId();
295  }

◆ clearHelperData()

clearHelperData ( )

Clear misc checkout parameters

Returns
void

Definition at line 441 of file Session.php.

442  {
443  $this->setRedirectUrl(null)->setLastOrderId(null)->setLastRealOrderId(null)->setAdditionalMessages(null);
444  }

◆ clearQuote()

clearQuote ( )

Destroy/end a session Unset all data associated with object

Returns
$this

Definition at line 415 of file Session.php.

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  }

◆ clearStorage()

clearStorage ( )

Unset all session data and quote

Returns
$this

Implements SessionManagerInterface.

Definition at line 429 of file Session.php.

430  {
431  parent::clearStorage();
432  $this->_quote = null;
433  return $this;
434  }

◆ getLastRealOrder()

getLastRealOrder ( )

Get order instance based on last order ID

Returns
\Magento\Sales\Model\Order

Definition at line 472 of file Session.php.

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  }

◆ getQuoteId()

getQuoteId ( )
Returns
int @codeCoverageIgnore

Definition at line 311 of file Session.php.

312  {
313  return $this->getData($this->_getQuoteIdKey());
314  }

◆ getStepData()

getStepData (   $step = null,
  $data = null 
)
Parameters
string | null$step
string | null$data
Returns
array|string|bool

Definition at line 391 of file Session.php.

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  }

◆ hasQuote()

hasQuote ( )

Check whether current session has quote

Returns
bool @codeCoverageIgnore

Definition at line 184 of file Session.php.

185  {
186  return isset($this->_quote);
187  }

◆ isQuoteMasked()

isQuoteMasked ( )
protected
Returns
bool|null @codeCoverageIgnore

Definition at line 522 of file Session.php.

523  {
524  return $this->isQuoteMasked;
525  }

◆ loadCustomerQuote()

loadCustomerQuote ( )

Load data for customer quote and merge with current quote

Returns
$this

Definition at line 321 of file Session.php.

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  }

◆ replaceQuote()

replaceQuote (   $quote)
Parameters
Quote$quote
Returns
$this

Definition at line 460 of file Session.php.

461  {
462  $this->_quote = $quote;
463  $this->setQuoteId($quote->getId());
464  return $this;
465  }
$quote

◆ resetCheckout()

resetCheckout ( )
Returns
$this @codeCoverageIgnore

Definition at line 450 of file Session.php.

451  {
452  $this->setCheckoutState(self::CHECKOUT_STATE_BEGIN);
453  return $this;
454  }

◆ setCustomerData()

setCustomerData (   $customer)

Set customer data.

Parameters
CustomerInterface | null$customer
Returns
\Magento\Checkout\Model\Session @codeCoverageIgnore

Definition at line 172 of file Session.php.

173  {
174  $this->_customer = $customer;
175  return $this;
176  }
$customer
Definition: customers.php:11

◆ setIsQuoteMasked()

setIsQuoteMasked (   $isQuoteMasked)
protected
Parameters
$isQuoteMaskedbool
Returns
void @codeCoverageIgnore

Definition at line 513 of file Session.php.

◆ setLoadInactive()

setLoadInactive (   $load = true)

Set quote to be loaded even if inactive

Parameters
bool$load
Returns
$this @codeCoverageIgnore

Definition at line 196 of file Session.php.

197  {
198  $this->_loadInactive = $load;
199  return $this;
200  }

◆ setQuoteId()

setQuoteId (   $quoteId)
Parameters
int$quoteId
Returns
void @codeCoverageIgnore

Definition at line 302 of file Session.php.

303  {
304  $this->storage->setData($this->_getQuoteIdKey(), $quoteId);
305  }

◆ setStepData()

setStepData (   $step,
  $data,
  $value = null 
)
Parameters
string$step
array | string$data
bool | string | null$value
Returns
$this

Definition at line 366 of file Session.php.

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  }
$value
Definition: gender.phtml:16

Field Documentation

◆ $_customer

$_customer
protected

Definition at line 36 of file Session.php.

◆ $_customerSession

$_customerSession
protected

Definition at line 60 of file Session.php.

◆ $_eventManager

$_eventManager
protected

Definition at line 75 of file Session.php.

◆ $_loadInactive

$_loadInactive = false
protected

Definition at line 43 of file Session.php.

◆ $_order

$_order
protected

Definition at line 50 of file Session.php.

◆ $_orderFactory

$_orderFactory
protected

Definition at line 55 of file Session.php.

◆ $_quote

$_quote
protected

Definition at line 29 of file Session.php.

◆ $_remoteAddress

$_remoteAddress
protected

Definition at line 70 of file Session.php.

◆ $_storeManager

$_storeManager
protected

Definition at line 80 of file Session.php.

◆ $customerRepository

$customerRepository
protected

Definition at line 85 of file Session.php.

◆ $isQuoteMasked

$isQuoteMasked
protected
Parameters
bool

Definition at line 95 of file Session.php.

◆ $quoteFactory

$quoteFactory
protected

Definition at line 100 of file Session.php.

◆ $quoteIdMaskFactory

$quoteIdMaskFactory
protected
Parameters
QuoteIdMaskFactory

Definition at line 90 of file Session.php.

◆ $quoteRepository

$quoteRepository
protected

Definition at line 65 of file Session.php.

◆ CHECKOUT_STATE_BEGIN

const CHECKOUT_STATE_BEGIN = 'begin'

Checkout state begin

Definition at line 22 of file Session.php.


The documentation for this class was generated from the following file: