Skip to content

Commit f41a7f6

Browse files
committed
MAGETWO-94347: Implement handling of large number of addresses on storefront Address book
1 parent 34416b1 commit f41a7f6

File tree

9 files changed

+685
-323
lines changed

9 files changed

+685
-323
lines changed

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

Lines changed: 40 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77

88
use Magento\Customer\Api\AddressRepositoryInterface;
99
use Magento\Customer\Model\Address\Mapper;
10-
use Magento\Framework\App\ObjectManager;
11-
use Magento\Directory\Model\CountryFactory;
12-
use Magento\Customer\Model\ResourceModel\Address\CollectionFactory as AddressCollectionFactory;
10+
use Magento\Customer\Block\Address\Grid as AddressesGrid;
1311

1412
/**
1513
* Customer address book block
@@ -25,6 +23,11 @@ class Book extends \Magento\Framework\View\Element\Template
2523
*/
2624
protected $currentCustomer;
2725

26+
/**
27+
* @var \Magento\Customer\Api\CustomerRepositoryInterface
28+
*/
29+
protected $customerRepository;
30+
2831
/**
2932
* @var AddressRepositoryInterface
3033
*/
@@ -41,32 +44,20 @@ class Book extends \Magento\Framework\View\Element\Template
4144
protected $addressMapper;
4245

4346
/**
44-
* @var \Magento\Customer\Model\ResourceModel\Address\CollectionFactory
45-
*/
46-
private $addressCollectionFactory;
47-
48-
/**
49-
* @var \Magento\Customer\Model\ResourceModel\Address\Collection
47+
* @var AddressesGrid
5048
*/
51-
private $addressCollection;
52-
53-
/**
54-
* @var CountryFactory
55-
*/
56-
private $countryFactory;
49+
private $addressesGrid;
5750

5851
/**
5952
* @param \Magento\Framework\View\Element\Template\Context $context
60-
* @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
53+
* @param CustomerRepositoryInterface|null $customerRepository
6154
* @param AddressRepositoryInterface $addressRepository
6255
* @param \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer
6356
* @param \Magento\Customer\Model\Address\Config $addressConfig
6457
* @param Mapper $addressMapper
6558
* @param array $data
66-
* @param AddressCollectionFactory|null $addressCollectionFactory
67-
* @param CountryFactory|null $countryFactory
59+
* @param AddressesGrid|null $addressesGrid
6860
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
69-
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
7061
*/
7162
public function __construct(
7263
\Magento\Framework\View\Element\Template\Context $context,
@@ -76,42 +67,37 @@ public function __construct(
7667
\Magento\Customer\Model\Address\Config $addressConfig,
7768
Mapper $addressMapper,
7869
array $data = [],
79-
AddressCollectionFactory $addressCollectionFactory = null,
80-
CountryFactory $countryFactory = null
70+
Grid $addressesGrid = null
8171
) {
8272
$this->currentCustomer = $currentCustomer;
8373
$this->addressRepository = $addressRepository;
8474
$this->_addressConfig = $addressConfig;
8575
$this->addressMapper = $addressMapper;
86-
$this->addressCollectionFactory = $addressCollectionFactory ?: ObjectManager::getInstance()
87-
->get(AddressCollectionFactory::class);
88-
$this->countryFactory = $countryFactory ?: ObjectManager::getInstance()->get(CountryFactory::class);
89-
76+
$this->addressesGrid = $addressesGrid ?: \Magento\Framework\App\ObjectManager::getInstance()
77+
->get(AddressesGrid::class);
9078
parent::__construct($context, $data);
9179
}
9280

9381
/**
9482
* Prepare the Address Book section layout
9583
*
9684
* @return $this
97-
* @throws \Magento\Framework\Exception\LocalizedException
9885
*/
9986
protected function _prepareLayout()
10087
{
10188
$this->pageConfig->getTitle()->set(__('Address Book'));
102-
parent::_prepareLayout();
103-
$this->preparePager();
104-
return $this;
89+
return parent::_prepareLayout();
10590
}
10691

10792
/**
10893
* Generate and return "New Address" URL
10994
*
11095
* @return string
96+
* @deprecated not used in this block (new block for additional addresses: \Magento\Customer\Block\Address\Grid
11197
*/
11298
public function getAddAddressUrl()
11399
{
114-
return $this->getUrl('customer/address/new', ['_secure' => true]);
100+
return $this->addressesGrid->getAddAddressUrl();
115101
}
116102

117103
/**
@@ -131,22 +117,25 @@ public function getBackUrl()
131117
* Generate and return "Delete" URL
132118
*
133119
* @return string
120+
* @deprecated not used in this block (new block for additional addresses: \Magento\Customer\Block\Address\Grid
134121
*/
135122
public function getDeleteUrl()
136123
{
137-
return $this->getUrl('customer/address/delete');
124+
return $this->addressesGrid->getDeleteUrl();
138125
}
139126

140127
/**
141128
* Generate and return "Edit Address" URL.
129+
*
142130
* Address ID passed in parameters
143131
*
144132
* @param int $addressId
145133
* @return string
134+
* @deprecated not used in this block (new block for additional addresses: \Magento\Customer\Block\Address\Grid
146135
*/
147136
public function getAddressEditUrl($addressId)
148137
{
149-
return $this->getUrl('customer/address/edit', ['_secure' => true, 'id' => $addressId]);
138+
return $this->addressesGrid->getAddressEditUrl($addressId);
150139
}
151140

152141
/**
@@ -162,33 +151,23 @@ public function hasPrimaryAddress()
162151

163152
/**
164153
* Get current additional customer addresses
154+
*
165155
* Will return array of address interfaces if customer have additional addresses and false in other case.
166156
*
167157
* @return \Magento\Customer\Api\Data\AddressInterface[]|bool
168158
* @throws \Magento\Framework\Exception\LocalizedException
159+
* @deprecated not used in this block (new block for additional addresses: \Magento\Customer\Block\Address\Grid
169160
*/
170161
public function getAdditionalAddresses()
171162
{
172-
try {
173-
$addresses = $this->getAddressCollection();
174-
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
175-
return false;
176-
}
177-
$primaryAddressIds = [$this->getDefaultBilling(), $this->getDefaultShipping()];
178-
foreach ($addresses as $address) {
179-
if (!in_array($address->getId(), $primaryAddressIds)) {
180-
$additional[] = $address->getDataModel();
181-
}
182-
}
183-
return empty($additional) ? false : $additional;
163+
return $this->addressesGrid->getAdditionalAddresses();
184164
}
185165

186166
/**
187167
* Render an address as HTML and return the result
188168
*
189169
* @param \Magento\Customer\Api\Data\AddressInterface $address
190170
* @return string
191-
* @deprecated Not used anymore as addresses are showed as a grid
192171
*/
193172
public function getAddressHtml(\Magento\Customer\Api\Data\AddressInterface $address = null)
194173
{
@@ -202,48 +181,28 @@ public function getAddressHtml(\Magento\Customer\Api\Data\AddressInterface $addr
202181

203182
/**
204183
* Get current customer
184+
*
205185
* Check if customer is stored in current object and return it
206186
* or get customer by current customer ID through repository
207187
*
208-
* @return \Magento\Customer\Api\Data\CustomerInterface
188+
* @return \Magento\Customer\Api\Data\CustomerInterface|null
209189
*/
210190
public function getCustomer()
211191
{
212-
$customer = $this->getData('customer');
213-
if ($customer === null) {
214-
$customer = $this->currentCustomer->getCustomer();
215-
$this->setData('customer', $customer);
216-
}
217-
return $customer;
192+
return $this->addressesGrid->getCustomer();
218193
}
219194

220195
/**
221-
* Get default billing address
222-
* Return address string if address found and null if not
223-
*
224-
* @return string
225-
* @throws \Magento\Framework\Exception\LocalizedException
196+
* @return int|null
226197
*/
227198
public function getDefaultBilling()
228199
{
229200
$customer = $this->getCustomer();
230-
231-
return $customer->getDefaultBilling();
232-
}
233-
234-
235-
/**
236-
* Get default shipping address
237-
* Return address string if address found and null of not
238-
*
239-
* @return string
240-
* @throws \Magento\Framework\Exception\LocalizedException
241-
*/
242-
public function getDefaultShipping()
243-
{
244-
$customer = $this->getCustomer();
245-
246-
return $customer->getDefaultShipping();
201+
if ($customer === null) {
202+
return null;
203+
} else {
204+
return $customer->getDefaultBilling();
205+
}
247206
}
248207

249208
/**
@@ -263,77 +222,15 @@ public function getAddressById($addressId)
263222
}
264223

265224
/**
266-
* Get one string street address from "two fields" array or just returns string if it was passed in parameters
267-
*
268-
* @param string|array $street
269-
* @return string
225+
* @return int|null
270226
*/
271-
public function getStreetAddress($street)
272-
{
273-
if (is_array($street)) {
274-
$street = implode(', ', $street);
275-
}
276-
return $street;
277-
}
278-
279-
/**
280-
* Get pager section HTML code
281-
*
282-
* @return string
283-
*/
284-
public function getPagerHtml()
285-
{
286-
return $this->getChildHtml('pager');
287-
}
288-
289-
/**
290-
* Get country name by $countryId
291-
* Using \Magento\Directory\Model\Country to get country name by $countryId
292-
*
293-
* @param string $countryId
294-
* @return string
295-
*/
296-
public function getCountryById($countryId)
297-
{
298-
/** @var \Magento\Directory\Model\Country $country */
299-
$country = $this->countryFactory->create();
300-
return $country->loadByCode($countryId)->getName();
301-
}
302-
303-
/**
304-
* Get pager layout
305-
*
306-
* @return void
307-
* @throws \Magento\Framework\Exception\LocalizedException
308-
*/
309-
private function preparePager()
310-
{
311-
$addressCollection = $this->getAddressCollection();
312-
if (null !== $addressCollection) {
313-
$pager = $this->getLayout()->createBlock(
314-
\Magento\Theme\Block\Html\Pager::class,
315-
'customer.addresses.pager'
316-
)->setCollection($addressCollection);
317-
$this->setChild('pager', $pager);
318-
}
319-
}
320-
321-
/**
322-
* Get customer addresses collection.
323-
* Filters collection by customer id
324-
*
325-
* @return \Magento\Customer\Model\ResourceModel\Address\Collection
326-
*/
327-
private function getAddressCollection()
227+
public function getDefaultShipping()
328228
{
329-
if (null === $this->addressCollection) {
330-
/** @var \Magento\Customer\Model\ResourceModel\Address\Collection $collection */
331-
$collection = $this->addressCollectionFactory->create();
332-
$collection->setOrder('entity_id', 'desc')
333-
->setCustomerFilter([$this->getCustomer()->getId()]);
334-
$this->addressCollection = $collection;
229+
$customer = $this->getCustomer();
230+
if ($customer === null) {
231+
return null;
232+
} else {
233+
return $customer->getDefaultShipping();
335234
}
336-
return $this->addressCollection;
337235
}
338-
339236
}

0 commit comments

Comments
 (0)