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