Skip to content

Commit 45e3ddb

Browse files
authored
Merge pull request #3591 from magento-panda/MAGETWO-94347
Fixed issues: - MAGETWO-95034 [SPIKE] Investigate ability of addresses grid displaying on frontend -MAGETWO-94347 Implement handling of large number of addresses on storefront Address book
2 parents b8e3a0b + 12fc8b2 commit 45e3ddb

File tree

21 files changed

+1149
-85
lines changed

21 files changed

+1149
-85
lines changed

app/code/Magento/Checkout/Test/Mftf/Test/StorefrontCustomerPlaceOrderWithNewAddressesThatWasEditedTest.xml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,12 @@
7979

8080
<!--Verify New addresses in Customer's Address Book-->
8181
<amOnPage url="{{StorefrontCustomerAddressesPage.url}}" stepKey="goToCustomerAddressBook"/>
82-
<see userInput="{{UK_Not_Default_Address.street[0]}} {{UK_Not_Default_Address.city}}, {{UK_Not_Default_Address.postcode}}"
83-
selector="{{StorefrontCustomerAddressesSection.addressesList}}" stepKey="checkNewAddresses"/>
82+
<see userInput="{{UK_Not_Default_Address.street[0]}}"
83+
selector="{{StorefrontCustomerAddressesSection.addressesList}}" stepKey="checkNewAddressesStreet"/>
84+
<see userInput="{{UK_Not_Default_Address.city}}"
85+
selector="{{StorefrontCustomerAddressesSection.addressesList}}" stepKey="checkNewAddressesCity"/>
86+
<see userInput="{{UK_Not_Default_Address.postcode}}"
87+
selector="{{StorefrontCustomerAddressesSection.addressesList}}" stepKey="checkNewAddressesPostcode"/>
8488
<!--Order review page has address that was created during checkout-->
8589
<amOnPage url="{{StorefrontCustomerOrderViewPage.url({$grabOrderNumber})}}" stepKey="goToOrderReviewPage"/>
8690
<see userInput="{{UK_Not_Default_Address.street[0]}} {{UK_Not_Default_Address.city}}, {{UK_Not_Default_Address.postcode}}"

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

Lines changed: 61 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
namespace Magento\Customer\Block\Address;
77

88
use Magento\Customer\Api\AddressRepositoryInterface;
9-
use Magento\Customer\Api\CustomerRepositoryInterface;
109
use Magento\Customer\Model\Address\Mapper;
10+
use Magento\Customer\Block\Address\Grid as AddressesGrid;
1111

1212
/**
1313
* Customer address book block
@@ -24,7 +24,7 @@ class Book extends \Magento\Framework\View\Element\Template
2424
protected $currentCustomer;
2525

2626
/**
27-
* @var CustomerRepositoryInterface
27+
* @var \Magento\Customer\Api\CustomerRepositoryInterface
2828
*/
2929
protected $customerRepository;
3030

@@ -43,33 +43,44 @@ class Book extends \Magento\Framework\View\Element\Template
4343
*/
4444
protected $addressMapper;
4545

46+
/**
47+
* @var AddressesGrid
48+
*/
49+
private $addressesGrid;
50+
4651
/**
4752
* @param \Magento\Framework\View\Element\Template\Context $context
48-
* @param CustomerRepositoryInterface $customerRepository
53+
* @param CustomerRepositoryInterface|null $customerRepository
4954
* @param AddressRepositoryInterface $addressRepository
5055
* @param \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer
5156
* @param \Magento\Customer\Model\Address\Config $addressConfig
5257
* @param Mapper $addressMapper
5358
* @param array $data
59+
* @param AddressesGrid|null $addressesGrid
60+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
5461
*/
5562
public function __construct(
5663
\Magento\Framework\View\Element\Template\Context $context,
57-
CustomerRepositoryInterface $customerRepository,
64+
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository = null,
5865
AddressRepositoryInterface $addressRepository,
5966
\Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,
6067
\Magento\Customer\Model\Address\Config $addressConfig,
6168
Mapper $addressMapper,
62-
array $data = []
69+
array $data = [],
70+
Grid $addressesGrid = null
6371
) {
64-
$this->customerRepository = $customerRepository;
6572
$this->currentCustomer = $currentCustomer;
6673
$this->addressRepository = $addressRepository;
6774
$this->_addressConfig = $addressConfig;
6875
$this->addressMapper = $addressMapper;
76+
$this->addressesGrid = $addressesGrid ?: \Magento\Framework\App\ObjectManager::getInstance()
77+
->get(AddressesGrid::class);
6978
parent::__construct($context, $data);
7079
}
7180

7281
/**
82+
* Prepare the Address Book section layout
83+
*
7384
* @return $this
7485
*/
7586
protected function _prepareLayout()
@@ -79,14 +90,20 @@ protected function _prepareLayout()
7990
}
8091

8192
/**
93+
* Generate and return "New Address" URL
94+
*
8295
* @return string
96+
* @deprecated not used in this block
97+
* @see \Magento\Customer\Block\Address\Grid::getAddAddressUrl
8398
*/
8499
public function getAddAddressUrl()
85100
{
86-
return $this->getUrl('customer/address/new', ['_secure' => true]);
101+
return $this->addressesGrid->getAddAddressUrl();
87102
}
88103

89104
/**
105+
* Generate and return "Back" URL
106+
*
90107
* @return string
91108
*/
92109
public function getBackUrl()
@@ -98,47 +115,60 @@ public function getBackUrl()
98115
}
99116

100117
/**
118+
* Generate and return "Delete" URL
119+
*
101120
* @return string
121+
* @deprecated not used in this block
122+
* @see \Magento\Customer\Block\Address\Grid::getDeleteUrl
102123
*/
103124
public function getDeleteUrl()
104125
{
105-
return $this->getUrl('customer/address/delete');
126+
return $this->addressesGrid->getDeleteUrl();
106127
}
107128

108129
/**
130+
* Generate and return "Edit Address" URL.
131+
*
132+
* Address ID passed in parameters
133+
*
109134
* @param int $addressId
110135
* @return string
136+
* @deprecated not used in this block
137+
* @see \Magento\Customer\Block\Address\Grid::getAddressEditUrl
111138
*/
112139
public function getAddressEditUrl($addressId)
113140
{
114-
return $this->getUrl('customer/address/edit', ['_secure' => true, 'id' => $addressId]);
141+
return $this->addressesGrid->getAddressEditUrl($addressId);
115142
}
116143

117144
/**
145+
* Determines is the address primary (billing or shipping)
146+
*
118147
* @return bool
148+
* @throws \Magento\Framework\Exception\LocalizedException
119149
*/
120150
public function hasPrimaryAddress()
121151
{
122152
return $this->getDefaultBilling() || $this->getDefaultShipping();
123153
}
124154

125155
/**
156+
* Get current additional customer addresses
157+
*
158+
* Will return array of address interfaces if customer have additional addresses and false in other case.
159+
*
126160
* @return \Magento\Customer\Api\Data\AddressInterface[]|bool
161+
* @throws \Magento\Framework\Exception\LocalizedException
162+
* @deprecated not used in this block
163+
* @see \Magento\Customer\Block\Address\Grid::getAdditionalAddresses
127164
*/
128165
public function getAdditionalAddresses()
129166
{
130167
try {
131-
$addresses = $this->customerRepository->getById($this->currentCustomer->getCustomerId())->getAddresses();
168+
$addresses = $this->addressesGrid->getAdditionalAddresses();
132169
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
133-
return false;
134-
}
135-
$primaryAddressIds = [$this->getDefaultBilling(), $this->getDefaultShipping()];
136-
foreach ($addresses as $address) {
137-
if (!in_array($address->getId(), $primaryAddressIds)) {
138-
$additional[] = $address;
139-
}
140170
}
141-
return empty($additional) ? false : $additional;
171+
return empty($addresses) ? false : $addresses;
142172
}
143173

144174
/**
@@ -158,23 +188,23 @@ public function getAddressHtml(\Magento\Customer\Api\Data\AddressInterface $addr
158188
}
159189

160190
/**
191+
* Get current customer
192+
*
161193
* @return \Magento\Customer\Api\Data\CustomerInterface|null
162194
*/
163195
public function getCustomer()
164196
{
165-
$customer = $this->getData('customer');
166-
if ($customer === null) {
167-
try {
168-
$customer = $this->customerRepository->getById($this->currentCustomer->getCustomerId());
169-
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
170-
return null;
171-
}
172-
$this->setData('customer', $customer);
197+
$customer = null;
198+
try {
199+
$customer = $this->currentCustomer->getCustomer();
200+
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
173201
}
174202
return $customer;
175203
}
176204

177205
/**
206+
* Get customer's default billing address
207+
*
178208
* @return int|null
179209
*/
180210
public function getDefaultBilling()
@@ -188,8 +218,11 @@ public function getDefaultBilling()
188218
}
189219

190220
/**
221+
* Get customer address by ID
222+
*
191223
* @param int $addressId
192224
* @return \Magento\Customer\Api\Data\AddressInterface|null
225+
* @throws \Magento\Framework\Exception\LocalizedException
193226
*/
194227
public function getAddressById($addressId)
195228
{
@@ -201,6 +234,8 @@ public function getAddressById($addressId)
201234
}
202235

203236
/**
237+
* Get customer's default shipping address
238+
*
204239
* @return int|null
205240
*/
206241
public function getDefaultShipping()

0 commit comments

Comments
 (0)