Skip to content

Commit 858b130

Browse files
committed
Merge remote-tracking branch 'tango/MC-17137' into PR-07-24-1
2 parents b9fd1df + d69ef9d commit 858b130

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

app/code/Magento/Persistent/Observer/CheckExpirePersistentQuoteObserver.php

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Magento\Persistent\Observer;
1010

1111
use Magento\Framework\Event\ObserverInterface;
12+
use Magento\Quote\Model\Quote;
1213

1314
/**
1415
* Observer of expired session
@@ -71,6 +72,11 @@ class CheckExpirePersistentQuoteObserver implements ObserverInterface
7172
*/
7273
private $checkoutPagePath = 'checkout';
7374

75+
/**
76+
* @var Quote
77+
*/
78+
private $quote;
79+
7480
/**
7581
* @param \Magento\Persistent\Helper\Session $persistentSession
7682
* @param \Magento\Persistent\Helper\Data $persistentData
@@ -110,23 +116,65 @@ public function execute(\Magento\Framework\Event\Observer $observer)
110116
return;
111117
}
112118

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+
113127
if ($this->_persistentData->isEnabled() &&
114128
!$this->_persistentSession->isPersistent() &&
115129
!$this->_customerSession->isLoggedIn() &&
116130
$this->_checkoutSession->getQuoteId() &&
117131
!$this->isRequestFromCheckoutPage($this->request) &&
118132
// persistent session does not expire on onepage checkout page
119-
(
120-
$this->_checkoutSession->getQuote()->getIsPersistent() ||
121-
$this->_checkoutSession->getQuote()->getCustomerIsGuest()
122-
)
133+
$this->isNeedToExpireSession()
123134
) {
124135
$this->_eventManager->dispatch('persistent_session_expired');
125136
$this->quoteManager->expire();
126137
$this->_customerSession->setCustomerId(null)->setCustomerGroupId(null);
127138
}
128139
}
129140

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+
130178
/**
131179
* Check current request is coming from onepage checkout page.
132180
*

app/code/Magento/Persistent/Test/Unit/Observer/CheckExpirePersistentQuoteObserverTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function testExecuteWhenPersistentIsNotEnabled()
112112
->method('canProcess')
113113
->with($this->observerMock)
114114
->will($this->returnValue(true));
115-
$this->persistentHelperMock->expects($this->once())->method('isEnabled')->will($this->returnValue(false));
115+
$this->persistentHelperMock->expects($this->exactly(2))->method('isEnabled')->will($this->returnValue(false));
116116
$this->eventManagerMock->expects($this->never())->method('dispatch');
117117
$this->model->execute($this->observerMock);
118118
}
@@ -139,7 +139,9 @@ public function testExecuteWhenPersistentIsEnabled(
139139
->method('canProcess')
140140
->with($this->observerMock)
141141
->will($this->returnValue(true));
142-
$this->persistentHelperMock->expects($this->once())->method('isEnabled')->will($this->returnValue(true));
142+
$this->persistentHelperMock->expects($this->atLeastOnce())
143+
->method('isEnabled')
144+
->will($this->returnValue(true));
143145
$this->sessionMock->expects($this->once())->method('isPersistent')->will($this->returnValue(false));
144146
$this->checkoutSessionMock
145147
->method('getQuote')

0 commit comments

Comments
 (0)