Skip to content

Commit 53d131a

Browse files
authored
Merge pull request #4687 from magento-tsg/MC-19568
[tsg] MC-19568: Worldpay: Product is missing from shopping cart after Worldpay cancellation
2 parents 4e95621 + 890d7dd commit 53d131a

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

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

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

1111
use Magento\Framework\Event\ObserverInterface;
12+
use Magento\Framework\Exception\NoSuchEntityException;
13+
use Magento\Quote\Api\CartRepositoryInterface;
14+
use Magento\Quote\Api\Data\CartInterface;
1215
use Magento\Quote\Model\Quote;
1316

1417
/**
@@ -77,6 +80,11 @@ class CheckExpirePersistentQuoteObserver implements ObserverInterface
7780
*/
7881
private $quote;
7982

83+
/**
84+
* @var CartRepositoryInterface
85+
*/
86+
private $quoteRepository;
87+
8088
/**
8189
* @param \Magento\Persistent\Helper\Session $persistentSession
8290
* @param \Magento\Persistent\Helper\Data $persistentData
@@ -85,6 +93,7 @@ class CheckExpirePersistentQuoteObserver implements ObserverInterface
8593
* @param \Magento\Customer\Model\Session $customerSession
8694
* @param \Magento\Checkout\Model\Session $checkoutSession
8795
* @param \Magento\Framework\App\RequestInterface $request
96+
* @param CartRepositoryInterface $quoteRepository
8897
*/
8998
public function __construct(
9099
\Magento\Persistent\Helper\Session $persistentSession,
@@ -93,7 +102,8 @@ public function __construct(
93102
\Magento\Framework\Event\ManagerInterface $eventManager,
94103
\Magento\Customer\Model\Session $customerSession,
95104
\Magento\Checkout\Model\Session $checkoutSession,
96-
\Magento\Framework\App\RequestInterface $request
105+
\Magento\Framework\App\RequestInterface $request,
106+
CartRepositoryInterface $quoteRepository
97107
) {
98108
$this->_persistentSession = $persistentSession;
99109
$this->quoteManager = $quoteManager;
@@ -102,6 +112,7 @@ public function __construct(
102112
$this->_eventManager = $eventManager;
103113
$this->_persistentData = $persistentData;
104114
$this->request = $request;
115+
$this->quoteRepository = $quoteRepository;
105116
}
106117

107118
/**
@@ -146,7 +157,9 @@ public function execute(\Magento\Framework\Event\Observer $observer)
146157
private function isPersistentQuoteOutdated(): bool
147158
{
148159
if (!$this->_persistentData->isEnabled() && !$this->_customerSession->isLoggedIn()
149-
&& $this->_checkoutSession->getQuoteId()) {
160+
&& $this->_checkoutSession->getQuoteId()
161+
&& $this->isActiveQuote()
162+
) {
150163
return (bool)$this->getQuote()->getIsPersistent();
151164
}
152165
return false;
@@ -175,6 +188,21 @@ private function getQuote(): Quote
175188
return $this->quote;
176189
}
177190

191+
/**
192+
* Check if quote is active.
193+
*
194+
* @return bool
195+
*/
196+
private function isActiveQuote(): bool
197+
{
198+
try {
199+
$this->quoteRepository->getActive($this->_checkoutSession->getQuoteId());
200+
return true;
201+
} catch (NoSuchEntityException $e) {
202+
return false;
203+
}
204+
}
205+
178206
/**
179207
* Check current request is coming from onepage checkout page.
180208
*

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
namespace Magento\Persistent\Test\Unit\Observer;
88

9+
use Magento\Framework\Exception\NoSuchEntityException;
10+
use Magento\Quote\Api\CartRepositoryInterface;
911
use Magento\Quote\Model\Quote;
1012

1113
/**
@@ -63,6 +65,11 @@ class CheckExpirePersistentQuoteObserverTest extends \PHPUnit\Framework\TestCase
6365
*/
6466
private $quoteMock;
6567

68+
/**
69+
* @var \PHPUnit_Framework_MockObject_MockObject|CartRepositoryInterface
70+
*/
71+
private $quoteRepositoryMock;
72+
6673
protected function setUp()
6774
{
6875
$this->sessionMock = $this->createMock(\Magento\Persistent\Helper\Session::class);
@@ -78,6 +85,7 @@ protected function setUp()
7885
->disableOriginalConstructor()
7986
->setMethods(['getRequestUri', 'getServer'])
8087
->getMockForAbstractClass();
88+
$this->quoteRepositoryMock = $this->createMock(CartRepositoryInterface::class);
8189

8290
$this->model = new \Magento\Persistent\Observer\CheckExpirePersistentQuoteObserver(
8391
$this->sessionMock,
@@ -86,7 +94,8 @@ protected function setUp()
8694
$this->eventManagerMock,
8795
$this->customerSessionMock,
8896
$this->checkoutSessionMock,
89-
$this->requestMock
97+
$this->requestMock,
98+
$this->quoteRepositoryMock
9099
);
91100
$this->quoteMock = $this->getMockBuilder(Quote::class)
92101
->setMethods(['getCustomerIsGuest', 'getIsPersistent'])
@@ -107,12 +116,19 @@ public function testExecuteWhenCanNotApplyPersistentData()
107116

108117
public function testExecuteWhenPersistentIsNotEnabled()
109118
{
119+
$quoteId = 'quote_id_1';
120+
110121
$this->persistentHelperMock
111122
->expects($this->once())
112123
->method('canProcess')
113124
->with($this->observerMock)
114125
->will($this->returnValue(true));
115126
$this->persistentHelperMock->expects($this->exactly(2))->method('isEnabled')->will($this->returnValue(false));
127+
$this->checkoutSessionMock->expects($this->exactly(2))->method('getQuoteId')->willReturn($quoteId);
128+
$this->quoteRepositoryMock->expects($this->once())
129+
->method('getActive')
130+
->with($quoteId)
131+
->willThrowException(new NoSuchEntityException());
116132
$this->eventManagerMock->expects($this->never())->method('dispatch');
117133
$this->model->execute($this->observerMock);
118134
}

0 commit comments

Comments
 (0)