Skip to content

Commit e4d5225

Browse files
committed
ACP2E-2470: Persistent shopping cart cleared during checkout step
1 parent be7b88d commit e4d5225

14 files changed

+228
-295
lines changed

app/code/Magento/Customer/Model/Customer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ public function getDataModel()
357357
\Magento\Customer\Api\Data\CustomerInterface::class
358358
);
359359
$customerDataObject->setAddresses($addressesData)
360-
->setId($this->getId());
360+
->setId($this->getId() ? (int) $this->getId() : null);
361361
return $customerDataObject;
362362
}
363363

app/code/Magento/Persistent/Model/Checkout/GuestPaymentInformationManagementPlugin.php

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,60 +7,43 @@
77
namespace Magento\Persistent\Model\Checkout;
88

99
use Magento\Checkout\Model\GuestPaymentInformationManagement;
10-
use Magento\Checkout\Model\Session;
1110

1211
/**
13-
* Plugin to convert shopping cart from persistent cart to guest cart before order save when customer not logged in
14-
*
1512
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
1613
*/
1714
class GuestPaymentInformationManagementPlugin
1815
{
1916
/**
20-
* Persistence Session Helper
21-
*
2217
* @var \Magento\Persistent\Helper\Session
2318
*/
2419
private $persistenceSessionHelper;
2520

2621
/**
27-
* Persistence Data Helper
28-
*
2922
* @var \Magento\Persistent\Helper\Data
3023
*/
3124
private $persistenceDataHelper;
3225

3326
/**
34-
* Customer Session
35-
*
3627
* @var \Magento\Customer\Model\Session
3728
*/
3829
private $customerSession;
3930

4031
/**
41-
* Checkout Session
42-
*
4332
* @var \Magento\Checkout\Model\Session
4433
*/
4534
private $checkoutSession;
4635

4736
/**
48-
* Quote Manager
49-
*
5037
* @var \Magento\Persistent\Model\QuoteManager
5138
*/
5239
private $quoteManager;
5340

5441
/**
55-
* Cart Repository
56-
*
5742
* @var \Magento\Quote\Api\CartRepositoryInterface
5843
*/
5944
private $cartRepository;
6045

6146
/**
62-
* Initialize dependencies
63-
*
6447
* @param \Magento\Persistent\Helper\Data $persistenceDataHelper
6548
* @param \Magento\Persistent\Helper\Session $persistenceSessionHelper
6649
* @param \Magento\Customer\Model\Session $customerSession
@@ -85,7 +68,7 @@ public function __construct(
8568
}
8669

8770
/**
88-
* Convert customer cart to guest cart before order is placed if customer is not logged in
71+
* Update customer email with the provided one
8972
*
9073
* @param GuestPaymentInformationManagement $subject
9174
* @param string $cartId
@@ -107,12 +90,9 @@ public function beforeSavePaymentInformation(
10790
&& $this->persistenceDataHelper->isShoppingCartPersist()
10891
&& $this->quoteManager->isPersistent()
10992
) {
110-
$this->customerSession->setCustomerId(null);
111-
$this->customerSession->setCustomerGroupId(null);
112-
$this->quoteManager->convertCustomerCartToGuest();
11393
$quoteId = $this->checkoutSession->getQuoteId();
11494
$quote = $this->cartRepository->get($quoteId);
115-
$quote->setCustomerEmail($email);
95+
$quote->setCustomerIsGuest(true);
11696
$quote->getAddressesCollection()->walk('setEmail', ['email' => $email]);
11797
$this->cartRepository->save($quote);
11898
}

app/code/Magento/Persistent/Model/Checkout/GuestShippingInformationManagementPlugin.php

Lines changed: 0 additions & 100 deletions
This file was deleted.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/************************************************************************
3+
*
4+
* Copyright 2024 Adobe
5+
* All Rights Reserved.
6+
*
7+
* NOTICE: All information contained herein is, and remains
8+
* the property of Adobe and its suppliers, if any. The intellectual
9+
* and technical concepts contained herein are proprietary to Adobe
10+
* and its suppliers and are protected by all applicable intellectual
11+
* property laws, including trade secret and copyright laws.
12+
* Dissemination of this information or reproduction of this material
13+
* is strictly forbidden unless prior written permission is obtained
14+
* from Adobe.
15+
* **********************************************************************
16+
*/
17+
declare(strict_types=1);
18+
19+
namespace Magento\Persistent\Model\Plugin;
20+
21+
use Magento\Customer\Model\Session as CustomerSession;
22+
use Magento\Persistent\Helper\Session as PersistentSession;
23+
use Magento\Persistent\Model\QuoteManager;
24+
use Magento\Quote\Model\Quote;
25+
use Magento\Quote\Model\QuoteManagement;
26+
27+
class ConvertCustomerCartToGuest
28+
{
29+
/**
30+
* @param CustomerSession $customerSession
31+
* @param PersistentSession $persistentSession
32+
* @param QuoteManager $quoteManager
33+
*/
34+
public function __construct(
35+
private readonly CustomerSession $customerSession,
36+
private readonly PersistentSession $persistentSession,
37+
private readonly QuoteManager $quoteManager
38+
) {
39+
}
40+
41+
/**
42+
* Convert customer cart to guest cart before order is placed if customer is not logged in
43+
*
44+
* @param QuoteManagement $subject
45+
* @param Quote $quote
46+
* @param array $orderData
47+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
48+
*/
49+
public function beforeSubmit(QuoteManagement $subject, Quote $quote, array $orderData = []): void
50+
{
51+
if ($quote->getIsPersistent() && $quote->getCustomerId() && $quote->getCustomerIsGuest()) {
52+
$this->customerSession->setCustomerId(null);
53+
$this->customerSession->setCustomerGroupId(null);
54+
$this->persistentSession->getSession()->removePersistentCookie();
55+
$this->persistentSession->setSession(null);
56+
$this->quoteManager->convertCustomerCartToGuest($quote);
57+
}
58+
}
59+
}

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

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -163,30 +163,24 @@ private function cleanCustomerData($quote)
163163
* Converts persistent cart tied to logged out customer to a guest cart, retaining customer information required for
164164
* checkout
165165
*
166+
* @param Quote $quote
166167
* @return void
167168
*/
168-
public function convertCustomerCartToGuest()
169+
public function convertCustomerCartToGuest(Quote $quote)
169170
{
170-
$quoteId = $this->checkoutSession->getQuoteId();
171-
/** @var $quote Quote */
172-
$quote = $this->quoteRepository->get($quoteId);
173-
if ($quote && $quote->getId()) {
174-
$this->_setQuotePersistent = false;
175-
$quote->setIsActive(true)
176-
->setCustomerId(null)
177-
->setCustomerEmail(null)
178-
->setCustomerFirstname(null)
179-
->setCustomerLastname(null)
180-
->setIsPersistent(false);
181-
$quote->getAddressesCollection()->walk('setCustomerAddressId', ['customerAddressId' => null]);
182-
$quote->getAddressesCollection()->walk('setCustomerId', ['customerId' => null]);
183-
$quote->getAddressesCollection()->walk('setEmail', ['email' => null]);
184-
$quote->collectTotals();
185-
$quote->getCustomer()->setId(null);
186-
$this->persistentSession->getSession()->removePersistentCookie();
187-
$this->persistentSession->setSession(null);
188-
$this->quoteRepository->save($quote);
189-
}
171+
$this->_setQuotePersistent = false;
172+
$billingAddress = $quote->getBillingAddress();
173+
$quote->setCustomerId(null)
174+
->setCustomerGroupId(GroupInterface::NOT_LOGGED_IN_ID)
175+
->setCustomerEmail($billingAddress->getEmail())
176+
->setCustomerFirstname($billingAddress->getFirstname())
177+
->setCustomerLastname($billingAddress->getLastname())
178+
->setIsPersistent(false);
179+
$quote->getAddressesCollection()->walk('setCustomerAddressId', ['customerAddressId' => null]);
180+
$quote->getAddressesCollection()->walk('setCustomerId', ['customerId' => null]);
181+
$quote->collectTotals();
182+
$quote->getCustomer()->setId(null);
183+
$this->quoteRepository->save($quote);
190184
}
191185

192186
/**

0 commit comments

Comments
 (0)