Skip to content

Commit 3f550cc

Browse files
committed
ACP2E-2166: Magento is slow for customers with large address books
- improvements
1 parent c1ee2f7 commit 3f550cc

File tree

7 files changed

+32
-20
lines changed

7 files changed

+32
-20
lines changed

app/code/Magento/Checkout/Block/Checkout/AttributeMerger.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Magento\Customer\Api\CustomerRepositoryInterface as CustomerRepository;
99
use Magento\Customer\Api\Data\CustomerInterface;
1010
use Magento\Customer\Helper\Address as AddressHelper;
11+
use Magento\Customer\Model\Customer;
1112
use Magento\Customer\Model\Session;
1213
use Magento\Directory\Helper\Data as DirectoryHelper;
1314
use Magento\Directory\Model\AllowedCountries;
@@ -78,7 +79,7 @@ class AttributeMerger
7879
private $customerRepository;
7980

8081
/**
81-
* @var CustomerInterface
82+
* @var Customer
8283
*/
8384
private $customer;
8485

@@ -380,13 +381,13 @@ protected function getDefaultValue($attributeCode): ?string
380381
*
381382
* @throws NoSuchEntityException
382383
* @throws LocalizedException
383-
* @return CustomerInterface|null
384+
* @return Customer|null
384385
*/
385-
protected function getCustomer(): ?CustomerInterface
386+
protected function getCustomer(): ?Customer
386387
{
387388
if (!$this->customer) {
388389
if ($this->customerSession->isLoggedIn()) {
389-
$this->customer = $this->customerRepository->getById($this->customerSession->getCustomerId());
390+
$this->customerSession->getCustomer();
390391
} else {
391392
return null;
392393
}

app/code/Magento/Checkout/Controller/Action.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function __construct(
6060
protected function _preDispatchValidateCustomer($redirect = true, $addErrors = true)
6161
{
6262
try {
63-
$customer = $this->customerRepository->getById($this->_customerSession->getCustomerId());
63+
$customer = $this->_customerSession->getCustomerData();
6464
} catch (NoSuchEntityException $e) {
6565
return true;
6666
}

app/code/Magento/Checkout/Model/DefaultConfigProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,6 @@ private function getQuoteAddressData(): array
775775
*/
776776
private function getCustomer(): CustomerInterface
777777
{
778-
return $this->customerRepository->getById($this->customerSession->getCustomerId());
778+
return $this->customerSession->getCustomerData();
779779
}
780780
}

app/code/Magento/Checkout/Model/Session.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ public function getQuote()
312312
if ($this->_customer) {
313313
$quote->setCustomer($this->_customer);
314314
} elseif ($this->_customerSession->isLoggedIn()) {
315-
$quote->setCustomer($this->customerRepository->getById($this->_customerSession->getCustomerId()));
315+
$quote->setCustomer($this->_customerSession->getCustomerData());
316316
}
317317

318318
$quote->setStore($this->_storeManager->getStore());

app/code/Magento/Checkout/Model/Type/Onepage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ public function initCheckout()
344344
* want to load the correct customer information by assigning to address
345345
* instead of just loading from sales/quote_address
346346
*/
347-
$customer = $customerSession->getCustomerDataObject();
347+
$customer = $customerSession->getCustomerData();
348348
if ($customer) {
349349
$quote->assignCustomer($customer);
350350
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -342,21 +342,21 @@ public function _construct()
342342
public function getDataModel()
343343
{
344344
$customerData = $this->getData();
345-
$addressesData = [];
346-
/** @var \Magento\Customer\Model\Address $address */
347-
foreach ($this->getAddresses() as $address) {
348-
if (!isset($this->storedAddress[$address->getId()])) {
349-
$this->storedAddress[$address->getId()] = $address->getDataModel();
345+
if (!$this->storedAddress) {
346+
/** @var \Magento\Customer\Model\Address $address */
347+
foreach ($this->getAddresses() as $address) {
348+
if (!isset($this->storedAddress[$address->getId()])) {
349+
$this->storedAddress[$address->getId()] = $address->getDataModel();
350+
}
350351
}
351-
$addressesData[] = $this->storedAddress[$address->getId()];
352352
}
353353
$customerDataObject = $this->customerDataFactory->create();
354354
$this->dataObjectHelper->populateWithArray(
355355
$customerDataObject,
356356
$customerData,
357357
\Magento\Customer\Api\Data\CustomerInterface::class
358358
);
359-
$customerDataObject->setAddresses($addressesData)
359+
$customerDataObject->setAddresses(array_values($this->storedAddress))
360360
->setId($this->getId());
361361
return $customerDataObject;
362362
}

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Customer\Model\Config\Share;
1414
use Magento\Customer\Model\ResourceModel\Customer as ResourceCustomer;
1515
use Magento\Framework\App\ObjectManager;
16+
use Magento\Framework\Exception\NoSuchEntityException;
1617
use Magento\Framework\Session\Generic;
1718

1819
/**
@@ -107,6 +108,11 @@ class Session extends \Magento\Framework\Session\SessionManager
107108
*/
108109
private $accountConfirmation;
109110

111+
/**
112+
* @var CustomerRegistry
113+
*/
114+
private $customerRegistry;
115+
110116
/**
111117
* Session constructor.
112118
*
@@ -132,6 +138,7 @@ class Session extends \Magento\Framework\Session\SessionManager
132138
* @param GroupManagementInterface $groupManagement
133139
* @param \Magento\Framework\App\Response\Http $response
134140
* @param AccountConfirmation $accountConfirmation
141+
* @param CustomerRegistry $customerRegistry
135142
* @throws \Magento\Framework\Exception\SessionException
136143
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
137144
*/
@@ -157,7 +164,8 @@ public function __construct(
157164
CustomerRepositoryInterface $customerRepository,
158165
GroupManagementInterface $groupManagement,
159166
\Magento\Framework\App\Response\Http $response,
160-
AccountConfirmation $accountConfirmation = null
167+
AccountConfirmation $accountConfirmation = null,
168+
CustomerRegistry $customerRegistry = null
161169
) {
162170
$this->_coreUrl = $coreUrl;
163171
$this->_customerUrl = $customerUrl;
@@ -173,6 +181,8 @@ public function __construct(
173181
$this->response = $response;
174182
$this->accountConfirmation = $accountConfirmation ?: ObjectManager::getInstance()
175183
->get(AccountConfirmation::class);
184+
$this->customerRegistry = $customerRegistry ?: ObjectManager::getInstance()
185+
->get(CustomerRegistry::class);
176186
parent::__construct(
177187
$request,
178188
$sidResolver,
@@ -299,14 +309,15 @@ public function setCustomer(Customer $customerModel)
299309
*
300310
* @return Customer
301311
* use getCustomerId() instead
312+
* @throws NoSuchEntityException
302313
*/
303314
public function getCustomer()
304315
{
305316
if ($this->_customerModel === null) {
306317
$this->_customerModel = $this->_customerFactory->create();
307318

308319
if ($this->getCustomerId()) {
309-
$this->_customerResource->load($this->_customerModel, $this->getCustomerId());
320+
$this->_customerModel = $this->customerRegistry->retrieve($this->getCustomerId());
310321
}
311322
}
312323

@@ -385,8 +396,8 @@ public function getCustomerGroupId()
385396
if ($this->storage->getData('customer_group_id')) {
386397
return $this->storage->getData('customer_group_id');
387398
}
388-
if ($this->getCustomerData()) {
389-
$customerGroupId = $this->getCustomerData()->getGroupId();
399+
if ($this->getCustomer()) {
400+
$customerGroupId = $this->getCustomer()->getGroupId();
390401
$this->setCustomerGroupId($customerGroupId);
391402
return $customerGroupId;
392403
}
@@ -431,7 +442,7 @@ public function checkCustomerId($customerId)
431442
}
432443

433444
try {
434-
$this->customerRepository->getById($customerId);
445+
$this->customerRegistry->retrieve($customerId);
435446
$this->_isCustomerIdChecked = $customerId;
436447
return true;
437448
} catch (\Exception $e) {

0 commit comments

Comments
 (0)