Skip to content

Commit 8d9fd51

Browse files
committed
MC-37109: Session Cache Local Storage | Checkout Issue
1 parent 5d8908c commit 8d9fd51

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

app/code/Magento/Persistent/Model/QuoteManager.php

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Persistent\Model;
77

8+
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
89
use Magento\Customer\Api\Data\GroupInterface;
910
use Magento\Framework\App\ObjectManager;
1011
use Magento\Persistent\Helper\Data;
@@ -64,21 +65,28 @@ class QuoteManager
6465
*/
6566
private $cartExtensionFactory;
6667

68+
/**
69+
* @var CustomerInterfaceFactory
70+
*/
71+
private $customerDataFactory;
72+
6773
/**
6874
* @param \Magento\Persistent\Helper\Session $persistentSession
6975
* @param Data $persistentData
7076
* @param \Magento\Checkout\Model\Session $checkoutSession
7177
* @param CartRepositoryInterface $quoteRepository
7278
* @param CartExtensionFactory|null $cartExtensionFactory
7379
* @param ShippingAssignmentProcessor|null $shippingAssignmentProcessor
80+
* @param CustomerInterfaceFactory|null $customerDataFactory
7481
*/
7582
public function __construct(
7683
\Magento\Persistent\Helper\Session $persistentSession,
7784
Data $persistentData,
7885
\Magento\Checkout\Model\Session $checkoutSession,
7986
CartRepositoryInterface $quoteRepository,
8087
?CartExtensionFactory $cartExtensionFactory = null,
81-
?ShippingAssignmentProcessor $shippingAssignmentProcessor = null
88+
?ShippingAssignmentProcessor $shippingAssignmentProcessor = null,
89+
?CustomerInterfaceFactory $customerDataFactory = null
8290
) {
8391
$this->persistentSession = $persistentSession;
8492
$this->persistentData = $persistentData;
@@ -88,6 +96,8 @@ public function __construct(
8896
?? ObjectManager::getInstance()->get(CartExtensionFactory::class);
8997
$this->shippingAssignmentProcessor = $shippingAssignmentProcessor
9098
?? ObjectManager::getInstance()->get(ShippingAssignmentProcessor::class);
99+
$this->customerDataFactory = $customerDataFactory
100+
?? ObjectManager::getInstance()->get(CustomerInterfaceFactory::class);
91101
}
92102

93103
/**
@@ -109,14 +119,11 @@ public function setGuest($checkQuote = false)
109119
$quote->getPaymentsCollection()->walk('delete');
110120
$quote->getAddressesCollection()->walk('delete');
111121
$this->_setQuotePersistent = false;
122+
$this->cleanCustomerData($quote);
112123
$quote->setIsActive(true)
113-
->setCustomerId(null)
114-
->setCustomerEmail(null)
115-
->setCustomerFirstname(null)
116-
->setCustomerLastname(null)
117-
->setCustomerGroupId(GroupInterface::NOT_LOGGED_IN_ID)
118124
->setIsPersistent(false)
119125
->removeAllAddresses();
126+
120127
//Create guest addresses
121128
$quote->getShippingAddress();
122129
$quote->getBillingAddress();
@@ -129,6 +136,27 @@ public function setGuest($checkQuote = false)
129136
$this->persistentSession->setSession(null);
130137
}
131138

139+
/**
140+
* Clear customer data in quote
141+
*
142+
* @param Quote $quote
143+
*/
144+
private function cleanCustomerData($quote)
145+
{
146+
/**
147+
* Set empty customer object in quote to avoid restore customer id
148+
* @see Quote::beforeSave()
149+
*/
150+
if ($quote->getCustomerId()) {
151+
$quote->setCustomer($this->customerDataFactory->create());
152+
}
153+
$quote->setCustomerId(null)
154+
->setCustomerEmail(null)
155+
->setCustomerFirstname(null)
156+
->setCustomerLastname(null)
157+
->setCustomerGroupId(GroupInterface::NOT_LOGGED_IN_ID);
158+
}
159+
132160
/**
133161
* Emulate guest cart with persistent cart
134162
*

app/code/Magento/Persistent/Test/Unit/Model/QuoteManagerTest.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
namespace Magento\Persistent\Test\Unit\Model;
1010

1111
use Magento\Checkout\Model\Session;
12+
use Magento\Customer\Api\Data\CustomerInterface;
13+
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
1214
use Magento\Customer\Model\GroupManagement;
1315
use Magento\Eav\Model\Entity\Collection\AbstractCollection;
1416
use Magento\Persistent\Helper\Data;
@@ -78,6 +80,11 @@ class QuoteManagerTest extends TestCase
7880
*/
7981
private $shippingAssignmentProcessor;
8082

83+
/**
84+
* @var CustomerInterfaceFactory|MockObject
85+
*/
86+
private $customerDataFactory;
87+
8188
protected function setUp(): void
8289
{
8390
$this->persistentSessionMock = $this->createMock(\Magento\Persistent\Helper\Session::class);
@@ -124,21 +131,24 @@ protected function setUp(): void
124131
'getItemsQty',
125132
'getExtensionAttributes',
126133
'setExtensionAttributes',
127-
'__wakeup'
134+
'__wakeup',
135+
'setCustomer'
128136
])
129137
->disableOriginalConstructor()
130138
->getMock();
131139

132140
$this->cartExtensionFactory = $this->createPartialMock(CartExtensionFactory::class, ['create']);
133141
$this->shippingAssignmentProcessor = $this->createPartialMock(ShippingAssignmentProcessor::class, ['create']);
142+
$this->customerDataFactory = $this->createMock(CustomerInterfaceFactory::class);
134143

135144
$this->model = new QuoteManager(
136145
$this->persistentSessionMock,
137146
$this->persistentDataMock,
138147
$this->checkoutSessionMock,
139148
$this->quoteRepositoryMock,
140149
$this->cartExtensionFactory,
141-
$this->shippingAssignmentProcessor
150+
$this->shippingAssignmentProcessor,
151+
$this->customerDataFactory
142152
);
143153
}
144154

@@ -189,6 +199,7 @@ public function testSetGuestWhenShoppingCartAndQuoteAreNotPersistent()
189199

190200
public function testSetGuest()
191201
{
202+
$customerId = 22;
192203
$this->checkoutSessionMock->expects($this->once())
193204
->method('getQuote')->willReturn($this->quoteMock);
194205
$this->quoteMock->expects($this->once())->method('getId')->willReturn(11);
@@ -220,6 +231,7 @@ public function testSetGuest()
220231
->method('getShippingAddress')->willReturn($quoteAddressMock);
221232
$this->quoteMock->expects($this->once())
222233
->method('getBillingAddress')->willReturn($quoteAddressMock);
234+
$this->quoteMock->method('getCustomerId')->willReturn($customerId);
223235
$this->quoteMock->expects($this->once())->method('collectTotals')->willReturn($this->quoteMock);
224236
$this->quoteRepositoryMock->expects($this->once())->method('save')->with($this->quoteMock);
225237
$this->persistentSessionMock->expects($this->once())
@@ -229,7 +241,6 @@ public function testSetGuest()
229241
$this->quoteMock->expects($this->once())->method('isVirtual')->willReturn(false);
230242
$this->quoteMock->expects($this->once())->method('getItemsQty')->willReturn(1);
231243
$extensionAttributes = $this->getMockBuilder(CartExtensionInterface::class)
232-
->addMethods(['getShippingAssignments', 'setShippingAssignments'])
233244
->getMockForAbstractClass();
234245
$shippingAssignment = $this->createMock(ShippingAssignmentInterface::class);
235246
$extensionAttributes->expects($this->once())
@@ -248,6 +259,11 @@ public function testSetGuest()
248259
$this->quoteMock->expects($this->once())
249260
->method('setExtensionAttributes')
250261
->with($extensionAttributes);
262+
$customerMock = $this->createMock(CustomerInterface::class);
263+
$this->customerDataFactory->method('create')->willReturn($customerMock);
264+
$this->quoteMock->expects($this->once())
265+
->method('setCustomer')
266+
->with($customerMock);
251267
$this->model->setGuest(false);
252268
}
253269

0 commit comments

Comments
 (0)