|
9 | 9 | namespace Magento\Persistent\Observer;
|
10 | 10 |
|
11 | 11 | use Magento\Framework\Event\ObserverInterface;
|
| 12 | +use Magento\Quote\Model\Quote; |
12 | 13 |
|
13 | 14 | /**
|
14 | 15 | * Observer of expired session
|
@@ -71,6 +72,11 @@ class CheckExpirePersistentQuoteObserver implements ObserverInterface
|
71 | 72 | */
|
72 | 73 | private $checkoutPagePath = 'checkout';
|
73 | 74 |
|
| 75 | + /** |
| 76 | + * @var Quote |
| 77 | + */ |
| 78 | + private $quote; |
| 79 | + |
74 | 80 | /**
|
75 | 81 | * @param \Magento\Persistent\Helper\Session $persistentSession
|
76 | 82 | * @param \Magento\Persistent\Helper\Data $persistentData
|
@@ -110,23 +116,65 @@ public function execute(\Magento\Framework\Event\Observer $observer)
|
110 | 116 | return;
|
111 | 117 | }
|
112 | 118 |
|
| 119 | + //clear persistent when persistent data is disabled |
| 120 | + if ($this->isPersistentQuoteOutdated()) { |
| 121 | + $this->_eventManager->dispatch('persistent_session_expired'); |
| 122 | + $this->quoteManager->expire(); |
| 123 | + $this->_checkoutSession->clearQuote(); |
| 124 | + return; |
| 125 | + } |
| 126 | + |
113 | 127 | if ($this->_persistentData->isEnabled() &&
|
114 | 128 | !$this->_persistentSession->isPersistent() &&
|
115 | 129 | !$this->_customerSession->isLoggedIn() &&
|
116 | 130 | $this->_checkoutSession->getQuoteId() &&
|
117 | 131 | !$this->isRequestFromCheckoutPage($this->request) &&
|
118 | 132 | // persistent session does not expire on onepage checkout page
|
119 |
| - ( |
120 |
| - $this->_checkoutSession->getQuote()->getIsPersistent() || |
121 |
| - $this->_checkoutSession->getQuote()->getCustomerIsGuest() |
122 |
| - ) |
| 133 | + $this->isNeedToExpireSession() |
123 | 134 | ) {
|
124 | 135 | $this->_eventManager->dispatch('persistent_session_expired');
|
125 | 136 | $this->quoteManager->expire();
|
126 | 137 | $this->_customerSession->setCustomerId(null)->setCustomerGroupId(null);
|
127 | 138 | }
|
128 | 139 | }
|
129 | 140 |
|
| 141 | + /** |
| 142 | + * Checks if current quote marked as persistent and Persistence Functionality is disabled. |
| 143 | + * |
| 144 | + * @return bool |
| 145 | + */ |
| 146 | + private function isPersistentQuoteOutdated(): bool |
| 147 | + { |
| 148 | + if (!$this->_persistentData->isEnabled() && !$this->_customerSession->isLoggedIn() |
| 149 | + && $this->_checkoutSession->getQuoteId()) { |
| 150 | + return (bool)$this->getQuote()->getIsPersistent(); |
| 151 | + } |
| 152 | + return false; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Condition checker |
| 157 | + * |
| 158 | + * @return bool |
| 159 | + */ |
| 160 | + private function isNeedToExpireSession(): bool |
| 161 | + { |
| 162 | + return $this->getQuote()->getIsPersistent() || $this->getQuote()->getCustomerIsGuest(); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Getter for Quote with micro optimization |
| 167 | + * |
| 168 | + * @return Quote |
| 169 | + */ |
| 170 | + private function getQuote(): Quote |
| 171 | + { |
| 172 | + if ($this->quote === null) { |
| 173 | + $this->quote = $this->_checkoutSession->getQuote(); |
| 174 | + } |
| 175 | + return $this->quote; |
| 176 | + } |
| 177 | + |
130 | 178 | /**
|
131 | 179 | * Check current request is coming from onepage checkout page.
|
132 | 180 | *
|
|
0 commit comments