Skip to content

Commit e7154fb

Browse files
committed
MAGETWO-97411: \Magento\Customer\Model\Customer::getDataModel method takes to much time to load with many addresses customer
1 parent d7ac52b commit e7154fb

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,9 @@ public function updateData(AddressInterface $address)
172172
public function getDataModel($defaultBillingAddressId = null, $defaultShippingAddressId = null)
173173
{
174174
if ($this->getCustomerId() || $this->getParentId()) {
175-
if ($this->getCustomer()->getDefaultBillingAddress()) {
176-
$defaultBillingAddressId = $this->getCustomer()->getDefaultBillingAddress()->getId();
177-
}
178-
if ($this->getCustomer()->getDefaultShippingAddress()) {
179-
$defaultShippingAddressId = $this->getCustomer()->getDefaultShippingAddress()->getId();
180-
}
175+
$primaryAddresses = $this->getCustomer()->getPrimaryAddressIds();
176+
$defaultBillingAddressId = $primaryAddresses['billing_address'] ?: $defaultBillingAddressId;
177+
$defaultShippingAddressId = $primaryAddresses['shipping_address'] ?: $defaultShippingAddressId;
181178
}
182179
return parent::getDataModel($defaultBillingAddressId, $defaultShippingAddressId);
183180
}

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,11 @@ class Customer extends \Magento\Framework\Model\AbstractModel
219219
*/
220220
private $accountConfirmation;
221221

222+
/**
223+
* @var array
224+
*/
225+
private $storedAddress;
226+
222227
/**
223228
* @param \Magento\Framework\Model\Context $context
224229
* @param \Magento\Framework\Registry $registry
@@ -311,18 +316,21 @@ public function _construct()
311316
public function getDataModel()
312317
{
313318
$customerData = $this->getData();
314-
$addressesData = [];
315-
/** @var \Magento\Customer\Model\Address $address */
316-
foreach ($this->getAddresses() as $address) {
317-
$addressesData[] = $address->getDataModel();
319+
if (!isset($this->storedAddress[$customerData['entity_id']])) {
320+
$addressesData = [];
321+
/** @var \Magento\Customer\Model\Address $address */
322+
foreach ($this->getAddresses() as $address) {
323+
$addressesData[] = $address->getDataModel();
324+
}
325+
$this->storedAddress[$customerData['entity_id']] = $addressesData;
318326
}
319327
$customerDataObject = $this->customerDataFactory->create();
320328
$this->dataObjectHelper->populateWithArray(
321329
$customerDataObject,
322330
$customerData,
323331
\Magento\Customer\Api\Data\CustomerInterface::class
324332
);
325-
$customerDataObject->setAddresses($addressesData)
333+
$customerDataObject->setAddresses($this->storedAddress[$customerData['entity_id']])
326334
->setId($this->getId());
327335
return $customerDataObject;
328336
}
@@ -676,10 +684,10 @@ public function getPrimaryAddressIds()
676684
{
677685
$ids = [];
678686
if ($this->getDefaultBilling()) {
679-
$ids[] = $this->getDefaultBilling();
687+
$ids['billing_address'] = $this->getDefaultBilling();
680688
}
681689
if ($this->getDefaultShipping()) {
682-
$ids[] = $this->getDefaultShipping();
690+
$ids['shipping_address'] = $this->getDefaultShipping();
683691
}
684692
return $ids;
685693
}

0 commit comments

Comments
 (0)