Skip to content

Commit 37bda71

Browse files
committed
MAGETWO-94347: Implement handling of large number of addresses on storefront Address book
1 parent 9567e16 commit 37bda71

File tree

3 files changed

+48
-82
lines changed

3 files changed

+48
-82
lines changed

app/code/Magento/Customer/Block/Address/Book.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ protected function _prepareLayout()
9393
* Generate and return "New Address" URL
9494
*
9595
* @return string
96-
* @deprecated not used in this block (new block for additional addresses: \Magento\Customer\Block\Address\Grid
96+
* @deprecated not used in this block
97+
* @see \Magento\Customer\Block\Address\Grid::getAddAddressUrl
9798
*/
9899
public function getAddAddressUrl()
99100
{
@@ -117,7 +118,8 @@ public function getBackUrl()
117118
* Generate and return "Delete" URL
118119
*
119120
* @return string
120-
* @deprecated not used in this block (new block for additional addresses: \Magento\Customer\Block\Address\Grid
121+
* @deprecated not used in this block
122+
* @see \Magento\Customer\Block\Address\Grid::getDeleteUrl
121123
*/
122124
public function getDeleteUrl()
123125
{
@@ -131,7 +133,8 @@ public function getDeleteUrl()
131133
*
132134
* @param int $addressId
133135
* @return string
134-
* @deprecated not used in this block (new block for additional addresses: \Magento\Customer\Block\Address\Grid
136+
* @deprecated not used in this block
137+
* @see \Magento\Customer\Block\Address\Grid::getAddressEditUrl
135138
*/
136139
public function getAddressEditUrl($addressId)
137140
{
@@ -156,11 +159,16 @@ public function hasPrimaryAddress()
156159
*
157160
* @return \Magento\Customer\Api\Data\AddressInterface[]|bool
158161
* @throws \Magento\Framework\Exception\LocalizedException
159-
* @deprecated not used in this block (new block for additional addresses: \Magento\Customer\Block\Address\Grid
162+
* @deprecated not used in this block
163+
* @see \Magento\Customer\Block\Address\Grid::getAdditionalAddresses
160164
*/
161165
public function getAdditionalAddresses()
162166
{
163-
return $this->addressesGrid->getAdditionalAddresses();
167+
try {
168+
$addresses = $this->addressesGrid->getAdditionalAddresses();
169+
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
170+
}
171+
return empty($addresses) ? false : $addresses;
164172
}
165173

166174
/**
@@ -182,14 +190,16 @@ public function getAddressHtml(\Magento\Customer\Api\Data\AddressInterface $addr
182190
/**
183191
* Get current customer
184192
*
185-
* Check if customer is stored in current object and return it
186-
* or get customer by current customer ID through repository
187-
*
188193
* @return \Magento\Customer\Api\Data\CustomerInterface|null
189194
*/
190195
public function getCustomer()
191196
{
192-
return $this->addressesGrid->getCustomer();
197+
$customer = null;
198+
try {
199+
$customer = $this->currentCustomer->getCustomer();
200+
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
201+
}
202+
return $customer;
193203
}
194204

195205
/**

app/code/Magento/Customer/Block/Address/Grid.php

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23
/**
34
* Copyright © Magento, Inc. All rights reserved.
45
* See COPYING.txt for license details.
@@ -58,22 +59,21 @@ public function __construct(
5859
/**
5960
* Prepare the Address Book section layout
6061
*
61-
* @return $this
62+
* @return void
6263
* @throws \Magento\Framework\Exception\LocalizedException
6364
*/
64-
protected function _prepareLayout()
65+
protected function _prepareLayout(): void
6566
{
6667
parent::_prepareLayout();
6768
$this->preparePager();
68-
return $this;
6969
}
7070

7171
/**
7272
* Generate and return "New Address" URL
7373
*
7474
* @return string
7575
*/
76-
public function getAddAddressUrl()
76+
public function getAddAddressUrl(): string
7777
{
7878
return $this->getUrl('customer/address/new', ['_secure' => true]);
7979
}
@@ -83,7 +83,7 @@ public function getAddAddressUrl()
8383
*
8484
* @return string
8585
*/
86-
public function getDeleteUrl()
86+
public function getDeleteUrl(): string
8787
{
8888
return $this->getUrl('customer/address/delete');
8989
}
@@ -96,7 +96,7 @@ public function getDeleteUrl()
9696
* @param int $addressId
9797
* @return string
9898
*/
99-
public function getAddressEditUrl($addressId)
99+
public function getAddressEditUrl($addressId): string
100100
{
101101
return $this->getUrl('customer/address/edit', ['_secure' => true, 'id' => $addressId]);
102102
}
@@ -106,42 +106,35 @@ public function getAddressEditUrl($addressId)
106106
*
107107
* Will return array of address interfaces if customer have additional addresses and false in other case.
108108
*
109-
* @return \Magento\Customer\Api\Data\AddressInterface[]|bool
109+
* @return \Magento\Customer\Api\Data\AddressInterface[]
110110
* @throws \Magento\Framework\Exception\LocalizedException
111+
* @throws NoSuchEntityException
111112
*/
112-
public function getAdditionalAddresses()
113+
public function getAdditionalAddresses(): array
113114
{
114-
try {
115-
$addresses = $this->getAddressCollection();
116-
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
117-
return false;
118-
}
115+
$additional = [];
116+
$addresses = $this->getAddressCollection();
119117
$primaryAddressIds = [$this->getDefaultBilling(), $this->getDefaultShipping()];
120118
foreach ($addresses as $address) {
121-
if (!in_array($address->getId(), $primaryAddressIds)) {
119+
if (!in_array((int)$address->getId(), $primaryAddressIds, true)) {
122120
$additional[] = $address->getDataModel();
123121
}
124122
}
125-
return empty($additional) ? false : $additional;
123+
return $additional;
126124
}
127125

128126
/**
129127
* Get current customer
130128
*
131-
* Check if customer is stored in current object and return it
132-
* or get customer by current customer ID through repository
129+
* Return stored customer or get it from session
133130
*
134-
* @return \Magento\Customer\Api\Data\CustomerInterface|null
131+
* @return \Magento\Customer\Api\Data\CustomerInterface
135132
*/
136-
public function getCustomer()
133+
public function getCustomer(): \Magento\Customer\Api\Data\CustomerInterface
137134
{
138135
$customer = $this->getData('customer');
139136
if ($customer === null) {
140-
try {
141-
$customer = $this->currentCustomer->getCustomer();
142-
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
143-
return null;
144-
}
137+
$customer = $this->currentCustomer->getCustomer();
145138
$this->setData('customer', $customer);
146139
}
147140
return $customer;
@@ -153,7 +146,7 @@ public function getCustomer()
153146
* @param string|array $street
154147
* @return string
155148
*/
156-
public function getStreetAddress($street)
149+
public function getStreetAddress($street): string
157150
{
158151
if (is_array($street)) {
159152
$street = implode(', ', $street);
@@ -169,7 +162,7 @@ public function getStreetAddress($street)
169162
* @param string $countryCode
170163
* @return string
171164
*/
172-
public function getCountryByCode($countryCode)
165+
public function getCountryByCode($countryCode): string
173166
{
174167
/** @var \Magento\Directory\Model\Country $country */
175168
$country = $this->countryFactory->create();
@@ -181,38 +174,38 @@ public function getCountryByCode($countryCode)
181174
* Get default billing address
182175
* Return address string if address found and null of not
183176
*
184-
* @return string
177+
* @return int
185178
* @throws \Magento\Framework\Exception\LocalizedException
186179
*/
187-
private function getDefaultBilling()
180+
private function getDefaultBilling(): int
188181
{
189182
$customer = $this->getCustomer();
190183

191-
return $customer->getDefaultBilling();
184+
return (int)$customer->getDefaultBilling();
192185
}
193186

194187

195188
/**
196189
* Get default shipping address
197190
* Return address string if address found and null of not
198191
*
199-
* @return string
192+
* @return int
200193
* @throws \Magento\Framework\Exception\LocalizedException
201194
*/
202-
private function getDefaultShipping()
195+
private function getDefaultShipping(): int
203196
{
204197
$customer = $this->getCustomer();
205198

206-
return $customer->getDefaultShipping();
199+
return (int)$customer->getDefaultShipping();
207200
}
208201

209202
/**
210203
* Get pager layout
211204
*
212-
* @return void
205+
* @return f
213206
* @throws \Magento\Framework\Exception\LocalizedException
214207
*/
215-
private function preparePager()
208+
private function preparePager(): void
216209
{
217210
$addressCollection = $this->getAddressCollection();
218211
if (null !== $addressCollection) {
@@ -232,7 +225,7 @@ private function preparePager()
232225
* @return \Magento\Customer\Model\ResourceModel\Address\Collection
233226
* @throws NoSuchEntityException
234227
*/
235-
private function getAddressCollection()
228+
private function getAddressCollection(): \Magento\Customer\Model\ResourceModel\Address\Collection
236229
{
237230
if (null === $this->addressCollection && $this->getCustomer()) {
238231
/** @var \Magento\Customer\Model\ResourceModel\Address\Collection $collection */

dev/tests/integration/testsuite/Magento/Customer/Block/Address/GridTest.php

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function testGetAdditionalAddressesNegative($customerId, $expected)
9494

9595
public function getAdditionalAddressesDataProvider()
9696
{
97-
return ['5' => [5, false]];
97+
return ['5' => [5, []]];
9898
}
9999

100100
/**
@@ -123,43 +123,6 @@ public function testGetCustomer()
123123
$this->assertEquals($customer, $object);
124124
}
125125

126-
/**
127-
* @magentoDataFixture Magento/Customer/_files/customer.php
128-
* @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
129-
* @magentoDataFixture Magento/Customer/_files/customer_no_address.php
130-
* @dataProvider getDefaultBillingDataProvider
131-
* @magentoAppIsolation enabled
132-
*/
133-
public function testGetDefaultBilling($customerId, $expected)
134-
{
135-
$gridBlock = $this->createBlockForCustomer($customerId);
136-
$this->assertEquals($expected, $gridBlock->getDefaultBilling());
137-
}
138-
139-
public function getDefaultBillingDataProvider()
140-
{
141-
return ['1' => [1, 1], '5' => [5, null]];
142-
}
143-
144-
/**
145-
* @magentoDataFixture Magento/Customer/_files/customer.php
146-
* @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
147-
* @magentoDataFixture Magento/Customer/_files/customer_no_address.php
148-
* @dataProvider getDefaultShippingDataProvider
149-
* @magentoAppIsolation enabled
150-
*/
151-
public function testGetDefaultShipping($customerId, $expected)
152-
{
153-
$gridBlock = $this->createBlockForCustomer($customerId);
154-
$this->currentCustomer->setCustomerId($customerId);
155-
$this->assertEquals($expected, $gridBlock->getDefaultShipping());
156-
}
157-
158-
public function getDefaultShippingDataProvider()
159-
{
160-
return ['1' => [1, 1], '5' => [5, null]];
161-
}
162-
163126
/**
164127
* Create address book block for customer
165128
*

0 commit comments

Comments
 (0)