7
7
8
8
use Magento \Customer \Api \AddressRepositoryInterface ;
9
9
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 ;
13
11
14
12
/**
15
13
* Customer address book block
@@ -25,6 +23,11 @@ class Book extends \Magento\Framework\View\Element\Template
25
23
*/
26
24
protected $ currentCustomer ;
27
25
26
+ /**
27
+ * @var \Magento\Customer\Api\CustomerRepositoryInterface
28
+ */
29
+ protected $ customerRepository ;
30
+
28
31
/**
29
32
* @var AddressRepositoryInterface
30
33
*/
@@ -41,32 +44,20 @@ class Book extends \Magento\Framework\View\Element\Template
41
44
protected $ addressMapper ;
42
45
43
46
/**
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
50
48
*/
51
- private $ addressCollection ;
52
-
53
- /**
54
- * @var CountryFactory
55
- */
56
- private $ countryFactory ;
49
+ private $ addressesGrid ;
57
50
58
51
/**
59
52
* @param \Magento\Framework\View\Element\Template\Context $context
60
- * @param \Magento\Customer\Api\ CustomerRepositoryInterface $customerRepository
53
+ * @param CustomerRepositoryInterface|null $customerRepository
61
54
* @param AddressRepositoryInterface $addressRepository
62
55
* @param \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer
63
56
* @param \Magento\Customer\Model\Address\Config $addressConfig
64
57
* @param Mapper $addressMapper
65
58
* @param array $data
66
- * @param AddressCollectionFactory|null $addressCollectionFactory
67
- * @param CountryFactory|null $countryFactory
59
+ * @param AddressesGrid|null $addressesGrid
68
60
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
69
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
70
61
*/
71
62
public function __construct (
72
63
\Magento \Framework \View \Element \Template \Context $ context ,
@@ -76,42 +67,37 @@ public function __construct(
76
67
\Magento \Customer \Model \Address \Config $ addressConfig ,
77
68
Mapper $ addressMapper ,
78
69
array $ data = [],
79
- AddressCollectionFactory $ addressCollectionFactory = null ,
80
- CountryFactory $ countryFactory = null
70
+ Grid $ addressesGrid = null
81
71
) {
82
72
$ this ->currentCustomer = $ currentCustomer ;
83
73
$ this ->addressRepository = $ addressRepository ;
84
74
$ this ->_addressConfig = $ addressConfig ;
85
75
$ 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);
90
78
parent ::__construct ($ context , $ data );
91
79
}
92
80
93
81
/**
94
82
* Prepare the Address Book section layout
95
83
*
96
84
* @return $this
97
- * @throws \Magento\Framework\Exception\LocalizedException
98
85
*/
99
86
protected function _prepareLayout ()
100
87
{
101
88
$ this ->pageConfig ->getTitle ()->set (__ ('Address Book ' ));
102
- parent ::_prepareLayout ();
103
- $ this ->preparePager ();
104
- return $ this ;
89
+ return parent ::_prepareLayout ();
105
90
}
106
91
107
92
/**
108
93
* Generate and return "New Address" URL
109
94
*
110
95
* @return string
96
+ * @deprecated not used in this block (new block for additional addresses: \Magento\Customer\Block\Address\Grid
111
97
*/
112
98
public function getAddAddressUrl ()
113
99
{
114
- return $ this ->getUrl ( ' customer/address/new ' , [ ' _secure ' => true ] );
100
+ return $ this ->addressesGrid -> getAddAddressUrl ( );
115
101
}
116
102
117
103
/**
@@ -131,22 +117,25 @@ public function getBackUrl()
131
117
* Generate and return "Delete" URL
132
118
*
133
119
* @return string
120
+ * @deprecated not used in this block (new block for additional addresses: \Magento\Customer\Block\Address\Grid
134
121
*/
135
122
public function getDeleteUrl ()
136
123
{
137
- return $ this ->getUrl ( ' customer/address/delete ' );
124
+ return $ this ->addressesGrid -> getDeleteUrl ( );
138
125
}
139
126
140
127
/**
141
128
* Generate and return "Edit Address" URL.
129
+ *
142
130
* Address ID passed in parameters
143
131
*
144
132
* @param int $addressId
145
133
* @return string
134
+ * @deprecated not used in this block (new block for additional addresses: \Magento\Customer\Block\Address\Grid
146
135
*/
147
136
public function getAddressEditUrl ($ addressId )
148
137
{
149
- return $ this ->getUrl ( ' customer/address/edit ' , [ ' _secure ' => true , ' id ' => $ addressId] );
138
+ return $ this ->addressesGrid -> getAddressEditUrl ( $ addressId );
150
139
}
151
140
152
141
/**
@@ -162,33 +151,23 @@ public function hasPrimaryAddress()
162
151
163
152
/**
164
153
* Get current additional customer addresses
154
+ *
165
155
* Will return array of address interfaces if customer have additional addresses and false in other case.
166
156
*
167
157
* @return \Magento\Customer\Api\Data\AddressInterface[]|bool
168
158
* @throws \Magento\Framework\Exception\LocalizedException
159
+ * @deprecated not used in this block (new block for additional addresses: \Magento\Customer\Block\Address\Grid
169
160
*/
170
161
public function getAdditionalAddresses ()
171
162
{
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 ();
184
164
}
185
165
186
166
/**
187
167
* Render an address as HTML and return the result
188
168
*
189
169
* @param \Magento\Customer\Api\Data\AddressInterface $address
190
170
* @return string
191
- * @deprecated Not used anymore as addresses are showed as a grid
192
171
*/
193
172
public function getAddressHtml (\Magento \Customer \Api \Data \AddressInterface $ address = null )
194
173
{
@@ -202,48 +181,28 @@ public function getAddressHtml(\Magento\Customer\Api\Data\AddressInterface $addr
202
181
203
182
/**
204
183
* Get current customer
184
+ *
205
185
* Check if customer is stored in current object and return it
206
186
* or get customer by current customer ID through repository
207
187
*
208
- * @return \Magento\Customer\Api\Data\CustomerInterface
188
+ * @return \Magento\Customer\Api\Data\CustomerInterface|null
209
189
*/
210
190
public function getCustomer ()
211
191
{
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 ();
218
193
}
219
194
220
195
/**
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
226
197
*/
227
198
public function getDefaultBilling ()
228
199
{
229
200
$ 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
+ }
247
206
}
248
207
249
208
/**
@@ -263,77 +222,15 @@ public function getAddressById($addressId)
263
222
}
264
223
265
224
/**
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
270
226
*/
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 ()
328
228
{
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 ();
335
234
}
336
- return $ this ->addressCollection ;
337
235
}
338
-
339
236
}
0 commit comments