Skip to content

Commit ccc5074

Browse files
committed
MC-19078: Product is missing from shopping cart after payment cancellation
1 parent d525030 commit ccc5074

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
namespace Magento\Persistent\Observer;
77

88
use Magento\Framework\Event\ObserverInterface;
9+
use Magento\Framework\Exception\NoSuchEntityException;
10+
use Magento\Quote\Api\CartRepositoryInterface;
11+
use Magento\Quote\Api\Data\CartInterface;
912
use Magento\Quote\Model\Quote;
1013

1114
/**
@@ -74,6 +77,11 @@ class CheckExpirePersistentQuoteObserver implements ObserverInterface
7477
*/
7578
private $quote;
7679

80+
/**
81+
* @var CartRepositoryInterface
82+
*/
83+
private $quoteRepository;
84+
7785
/**
7886
* @param \Magento\Persistent\Helper\Session $persistentSession
7987
* @param \Magento\Persistent\Helper\Data $persistentData
@@ -82,6 +90,7 @@ class CheckExpirePersistentQuoteObserver implements ObserverInterface
8290
* @param \Magento\Customer\Model\Session $customerSession
8391
* @param \Magento\Checkout\Model\Session $checkoutSession
8492
* @param \Magento\Framework\App\RequestInterface $request
93+
* @param CartRepositoryInterface $quoteRepository
8594
*/
8695
public function __construct(
8796
\Magento\Persistent\Helper\Session $persistentSession,
@@ -90,7 +99,8 @@ public function __construct(
9099
\Magento\Framework\Event\ManagerInterface $eventManager,
91100
\Magento\Customer\Model\Session $customerSession,
92101
\Magento\Checkout\Model\Session $checkoutSession,
93-
\Magento\Framework\App\RequestInterface $request
102+
\Magento\Framework\App\RequestInterface $request,
103+
CartRepositoryInterface $quoteRepository
94104
) {
95105
$this->_persistentSession = $persistentSession;
96106
$this->quoteManager = $quoteManager;
@@ -99,6 +109,7 @@ public function __construct(
99109
$this->_eventManager = $eventManager;
100110
$this->_persistentData = $persistentData;
101111
$this->request = $request;
112+
$this->quoteRepository = $quoteRepository;
102113
}
103114

104115
/**
@@ -146,9 +157,11 @@ public function execute(\Magento\Framework\Event\Observer $observer)
146157
*/
147158
private function isPersistentQuoteOutdated(): bool
148159
{
149-
if ((!$this->_persistentData->isEnabled() || !$this->_persistentData->isShoppingCartPersist())
160+
if (!($this->_persistentData->isEnabled() && $this->_persistentData->isShoppingCartPersist())
150161
&& !$this->_customerSession->isLoggedIn()
151-
&& $this->_checkoutSession->getQuoteId()) {
162+
&& $this->_checkoutSession->getQuoteId()
163+
&& $this->isActiveQuote()
164+
) {
152165
return (bool)$this->getQuote()->getIsPersistent();
153166
}
154167
return false;
@@ -181,6 +194,21 @@ private function getQuote(): Quote
181194
return $this->quote;
182195
}
183196

197+
/**
198+
* Check if quote is active.
199+
*
200+
* @return bool
201+
*/
202+
private function isActiveQuote(): bool
203+
{
204+
try {
205+
$this->quoteRepository->getActive($this->_checkoutSession->getQuoteId());
206+
return true;
207+
} catch (NoSuchEntityException $e) {
208+
return false;
209+
}
210+
}
211+
184212
/**
185213
* Check current request is coming from onepage checkout page.
186214
*

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
/**
6774
* @inheritdoc
6875
*/
@@ -82,6 +89,7 @@ protected function setUp()
8289
->disableOriginalConstructor()
8390
->setMethods(['getRequestUri', 'getServer'])
8491
->getMockForAbstractClass();
92+
$this->quoteRepositoryMock = $this->createMock(CartRepositoryInterface::class);
8593

8694
$this->model = new \Magento\Persistent\Observer\CheckExpirePersistentQuoteObserver(
8795
$this->sessionMock,
@@ -90,7 +98,8 @@ protected function setUp()
9098
$this->eventManagerMock,
9199
$this->customerSessionMock,
92100
$this->checkoutSessionMock,
93-
$this->requestMock
101+
$this->requestMock,
102+
$this->quoteRepositoryMock
94103
);
95104
$this->quoteMock = $this->getMockBuilder(Quote::class)
96105
->setMethods(['getCustomerIsGuest', 'getIsPersistent'])
@@ -111,12 +120,19 @@ public function testExecuteWhenCanNotApplyPersistentData()
111120

112121
public function testExecuteWhenPersistentIsNotEnabled()
113122
{
123+
$quoteId = 'quote_id_1';
124+
114125
$this->persistentHelperMock
115126
->expects($this->once())
116127
->method('canProcess')
117128
->with($this->observerMock)
118129
->willReturn(true);
119130
$this->persistentHelperMock->expects($this->exactly(2))->method('isEnabled')->willReturn(false);
131+
$this->checkoutSessionMock->expects($this->exactly(2))->method('getQuoteId')->willReturn($quoteId);
132+
$this->quoteRepositoryMock->expects($this->once())
133+
->method('getActive')
134+
->with($quoteId)
135+
->willThrowException(new NoSuchEntityException());
120136
$this->eventManagerMock->expects($this->never())->method('dispatch');
121137
$this->model->execute($this->observerMock);
122138
}

0 commit comments

Comments
 (0)