6
6
namespace Magento \Customer \Block \Address ;
7
7
8
8
use Magento \Customer \Api \AddressRepositoryInterface ;
9
- use Magento \Customer \Api \CustomerRepositoryInterface ;
10
9
use Magento \Customer \Model \Address \Mapper ;
10
+ use Magento \Customer \Block \Address \Grid as AddressesGrid ;
11
11
12
12
/**
13
13
* Customer address book block
@@ -24,7 +24,7 @@ class Book extends \Magento\Framework\View\Element\Template
24
24
protected $ currentCustomer ;
25
25
26
26
/**
27
- * @var CustomerRepositoryInterface
27
+ * @var \Magento\Customer\Api\ CustomerRepositoryInterface
28
28
*/
29
29
protected $ customerRepository ;
30
30
@@ -43,33 +43,44 @@ class Book extends \Magento\Framework\View\Element\Template
43
43
*/
44
44
protected $ addressMapper ;
45
45
46
+ /**
47
+ * @var AddressesGrid
48
+ */
49
+ private $ addressesGrid ;
50
+
46
51
/**
47
52
* @param \Magento\Framework\View\Element\Template\Context $context
48
- * @param CustomerRepositoryInterface $customerRepository
53
+ * @param CustomerRepositoryInterface|null $customerRepository
49
54
* @param AddressRepositoryInterface $addressRepository
50
55
* @param \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer
51
56
* @param \Magento\Customer\Model\Address\Config $addressConfig
52
57
* @param Mapper $addressMapper
53
58
* @param array $data
59
+ * @param AddressesGrid|null $addressesGrid
60
+ * @SuppressWarnings(PHPMD.UnusedFormalParameter)
54
61
*/
55
62
public function __construct (
56
63
\Magento \Framework \View \Element \Template \Context $ context ,
57
- CustomerRepositoryInterface $ customerRepository ,
64
+ \ Magento \ Customer \ Api \ CustomerRepositoryInterface $ customerRepository = null ,
58
65
AddressRepositoryInterface $ addressRepository ,
59
66
\Magento \Customer \Helper \Session \CurrentCustomer $ currentCustomer ,
60
67
\Magento \Customer \Model \Address \Config $ addressConfig ,
61
68
Mapper $ addressMapper ,
62
- array $ data = []
69
+ array $ data = [],
70
+ Grid $ addressesGrid = null
63
71
) {
64
- $ this ->customerRepository = $ customerRepository ;
65
72
$ this ->currentCustomer = $ currentCustomer ;
66
73
$ this ->addressRepository = $ addressRepository ;
67
74
$ this ->_addressConfig = $ addressConfig ;
68
75
$ this ->addressMapper = $ addressMapper ;
76
+ $ this ->addressesGrid = $ addressesGrid ?: \Magento \Framework \App \ObjectManager::getInstance ()
77
+ ->get (AddressesGrid::class);
69
78
parent ::__construct ($ context , $ data );
70
79
}
71
80
72
81
/**
82
+ * Prepare the Address Book section layout
83
+ *
73
84
* @return $this
74
85
*/
75
86
protected function _prepareLayout ()
@@ -79,14 +90,20 @@ protected function _prepareLayout()
79
90
}
80
91
81
92
/**
93
+ * Generate and return "New Address" URL
94
+ *
82
95
* @return string
96
+ * @deprecated not used in this block
97
+ * @see \Magento\Customer\Block\Address\Grid::getAddAddressUrl
83
98
*/
84
99
public function getAddAddressUrl ()
85
100
{
86
- return $ this ->getUrl ( ' customer/address/new ' , [ ' _secure ' => true ] );
101
+ return $ this ->addressesGrid -> getAddAddressUrl ( );
87
102
}
88
103
89
104
/**
105
+ * Generate and return "Back" URL
106
+ *
90
107
* @return string
91
108
*/
92
109
public function getBackUrl ()
@@ -98,47 +115,60 @@ public function getBackUrl()
98
115
}
99
116
100
117
/**
118
+ * Generate and return "Delete" URL
119
+ *
101
120
* @return string
121
+ * @deprecated not used in this block
122
+ * @see \Magento\Customer\Block\Address\Grid::getDeleteUrl
102
123
*/
103
124
public function getDeleteUrl ()
104
125
{
105
- return $ this ->getUrl ( ' customer/address/delete ' );
126
+ return $ this ->addressesGrid -> getDeleteUrl ( );
106
127
}
107
128
108
129
/**
130
+ * Generate and return "Edit Address" URL.
131
+ *
132
+ * Address ID passed in parameters
133
+ *
109
134
* @param int $addressId
110
135
* @return string
136
+ * @deprecated not used in this block
137
+ * @see \Magento\Customer\Block\Address\Grid::getAddressEditUrl
111
138
*/
112
139
public function getAddressEditUrl ($ addressId )
113
140
{
114
- return $ this ->getUrl ( ' customer/address/edit ' , [ ' _secure ' => true , ' id ' => $ addressId] );
141
+ return $ this ->addressesGrid -> getAddressEditUrl ( $ addressId );
115
142
}
116
143
117
144
/**
145
+ * Determines is the address primary (billing or shipping)
146
+ *
118
147
* @return bool
148
+ * @throws \Magento\Framework\Exception\LocalizedException
119
149
*/
120
150
public function hasPrimaryAddress ()
121
151
{
122
152
return $ this ->getDefaultBilling () || $ this ->getDefaultShipping ();
123
153
}
124
154
125
155
/**
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
+ *
126
160
* @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
127
164
*/
128
165
public function getAdditionalAddresses ()
129
166
{
130
167
try {
131
- $ addresses = $ this ->customerRepository -> getById ( $ this -> currentCustomer -> getCustomerId ())-> getAddresses ();
168
+ $ addresses = $ this ->addressesGrid -> getAdditionalAddresses ();
132
169
} 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
- }
140
170
}
141
- return empty ($ additional ) ? false : $ additional ;
171
+ return empty ($ addresses ) ? false : $ addresses ;
142
172
}
143
173
144
174
/**
@@ -158,23 +188,23 @@ public function getAddressHtml(\Magento\Customer\Api\Data\AddressInterface $addr
158
188
}
159
189
160
190
/**
191
+ * Get current customer
192
+ *
161
193
* @return \Magento\Customer\Api\Data\CustomerInterface|null
162
194
*/
163
195
public function getCustomer ()
164
196
{
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 ) {
173
201
}
174
202
return $ customer ;
175
203
}
176
204
177
205
/**
206
+ * Get customer's default billing address
207
+ *
178
208
* @return int|null
179
209
*/
180
210
public function getDefaultBilling ()
@@ -188,8 +218,11 @@ public function getDefaultBilling()
188
218
}
189
219
190
220
/**
221
+ * Get customer address by ID
222
+ *
191
223
* @param int $addressId
192
224
* @return \Magento\Customer\Api\Data\AddressInterface|null
225
+ * @throws \Magento\Framework\Exception\LocalizedException
193
226
*/
194
227
public function getAddressById ($ addressId )
195
228
{
@@ -201,6 +234,8 @@ public function getAddressById($addressId)
201
234
}
202
235
203
236
/**
237
+ * Get customer's default shipping address
238
+ *
204
239
* @return int|null
205
240
*/
206
241
public function getDefaultShipping ()
0 commit comments