Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CheckExpirePersistentQuoteObserver.php
Go to the documentation of this file.
1 <?php
8 
10 
12 {
18  protected $_customerSession;
19 
25  protected $_checkoutSession;
26 
32  protected $_eventManager = null;
33 
39  protected $_persistentSession = null;
40 
44  protected $quoteManager;
45 
51  protected $_persistentData = null;
52 
58  private $request;
59 
65  private $checkoutPagePath = 'checkout';
66 
76  public function __construct(
77  \Magento\Persistent\Helper\Session $persistentSession,
78  \Magento\Persistent\Helper\Data $persistentData,
79  \Magento\Persistent\Model\QuoteManager $quoteManager,
80  \Magento\Framework\Event\ManagerInterface $eventManager,
81  \Magento\Customer\Model\Session $customerSession,
82  \Magento\Checkout\Model\Session $checkoutSession,
83  \Magento\Framework\App\RequestInterface $request
84  ) {
85  $this->_persistentSession = $persistentSession;
86  $this->quoteManager = $quoteManager;
87  $this->_customerSession = $customerSession;
88  $this->_checkoutSession = $checkoutSession;
89  $this->_eventManager = $eventManager;
90  $this->_persistentData = $persistentData;
91  $this->request = $request;
92  }
93 
100  public function execute(\Magento\Framework\Event\Observer $observer)
101  {
102  if (!$this->_persistentData->canProcess($observer)) {
103  return;
104  }
105 
106  if ($this->_persistentData->isEnabled() &&
107  !$this->_persistentSession->isPersistent() &&
108  !$this->_customerSession->isLoggedIn() &&
109  $this->_checkoutSession->getQuoteId() &&
110  !$this->isRequestFromCheckoutPage($this->request)
111  // persistent session does not expire on onepage checkout page
112  ) {
113  $this->_eventManager->dispatch('persistent_session_expired');
114  $this->quoteManager->expire();
115  $this->_customerSession->setCustomerId(null)->setCustomerGroupId(null);
116  }
117  }
118 
125  private function isRequestFromCheckoutPage(\Magento\Framework\App\RequestInterface $request): bool
126  {
127  $requestUri = (string)$request->getRequestUri();
128  $refererUri = (string)$request->getServer('HTTP_REFERER');
129 
131  $isCheckoutPage = (
132  false !== strpos($requestUri, $this->checkoutPagePath) ||
133  false !== strpos($refererUri, $this->checkoutPagePath)
134  );
135 
136  return $isCheckoutPage;
137  }
138 }
$persistentSession
Definition: persistent.php:11
__construct(\Magento\Persistent\Helper\Session $persistentSession, \Magento\Persistent\Helper\Data $persistentData, \Magento\Persistent\Model\QuoteManager $quoteManager, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Customer\Model\Session $customerSession, \Magento\Checkout\Model\Session $checkoutSession, \Magento\Framework\App\RequestInterface $request)