5
5
*/
6
6
namespace Magento \CustomerImportExport \Model \Export ;
7
7
8
+ use Magento \Customer \Model \ResourceModel \Address \Collection ;
9
+ use Magento \Customer \Model \ResourceModel \Address \CollectionFactory ;
10
+ use Magento \Eav \Model \Config ;
11
+ use Magento \Framework \App \Config \ScopeConfigInterface ;
12
+ use Magento \Framework \DB \Select ;
13
+ use Magento \Framework \Stdlib \DateTime \TimezoneInterface ;
14
+ use Magento \ImportExport \Model \Export \Entity \AbstractEav ;
15
+ use Magento \ImportExport \Model \Export \Factory ;
16
+ use Magento \ImportExport \Model \ResourceModel \CollectionByPagesIteratorFactory ;
17
+ use Magento \Store \Model \StoreManagerInterface ;
18
+
8
19
/**
9
20
* Customer address export
10
21
*
13
24
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
14
25
* @since 100.0.2
15
26
*/
16
- class Address extends \ Magento \ ImportExport \ Model \ Export \ Entity \ AbstractEav
27
+ class Address extends AbstractEav
17
28
{
18
29
/**#@+
19
30
* Permanent column names
@@ -93,7 +104,7 @@ class Address extends \Magento\ImportExport\Model\Export\Entity\AbstractEav
93
104
/**
94
105
* Customer addresses collection
95
106
*
96
- * @var \Magento\Customer\Model\ResourceModel\Address\ Collection
107
+ * @var Collection
97
108
*/
98
109
protected $ _addressCollection ;
99
110
@@ -118,31 +129,31 @@ class Address extends \Magento\ImportExport\Model\Export\Entity\AbstractEav
118
129
*
119
130
* @var array
120
131
*/
121
- protected $ _customers = [] ;
132
+ protected $ _customers ;
122
133
123
134
/**
124
- * @param \Magento\Framework\App\Config\ ScopeConfigInterface $scopeConfig
125
- * @param \Magento\Store\Model\ StoreManagerInterface $storeManager
126
- * @param \Magento\ImportExport\Model\Export\ Factory $collectionFactory
127
- * @param \Magento\ImportExport\Model\ResourceModel\ CollectionByPagesIteratorFactory $resourceColFactory
128
- * @param \Magento\Framework\Stdlib\DateTime\ TimezoneInterface $localeDate
129
- * @param \Magento\Eav\Model\ Config $eavConfig
135
+ * @param ScopeConfigInterface $scopeConfig
136
+ * @param StoreManagerInterface $storeManager
137
+ * @param Factory $collectionFactory
138
+ * @param CollectionByPagesIteratorFactory $resourceColFactory
139
+ * @param TimezoneInterface $localeDate
140
+ * @param Config $eavConfig
130
141
* @param \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerColFactory
131
- * @param \Magento\CustomerImportExport\Model\Export\ CustomerFactory $eavCustomerFactory
132
- * @param \Magento\Customer\Model\ResourceModel\Address\ CollectionFactory $addressColFactory
142
+ * @param CustomerFactory $eavCustomerFactory
143
+ * @param CollectionFactory $addressColFactory
133
144
* @param array $data
134
145
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
135
146
*/
136
147
public function __construct (
137
- \ Magento \ Framework \ App \ Config \ ScopeConfigInterface $ scopeConfig ,
138
- \ Magento \ Store \ Model \ StoreManagerInterface $ storeManager ,
139
- \ Magento \ ImportExport \ Model \ Export \ Factory $ collectionFactory ,
140
- \ Magento \ ImportExport \ Model \ ResourceModel \ CollectionByPagesIteratorFactory $ resourceColFactory ,
141
- \ Magento \ Framework \ Stdlib \ DateTime \ TimezoneInterface $ localeDate ,
142
- \ Magento \ Eav \ Model \ Config $ eavConfig ,
148
+ ScopeConfigInterface $ scopeConfig ,
149
+ StoreManagerInterface $ storeManager ,
150
+ Factory $ collectionFactory ,
151
+ CollectionByPagesIteratorFactory $ resourceColFactory ,
152
+ TimezoneInterface $ localeDate ,
153
+ Config $ eavConfig ,
143
154
\Magento \Customer \Model \ResourceModel \Customer \CollectionFactory $ customerColFactory ,
144
- \ Magento \ CustomerImportExport \ Model \ Export \ CustomerFactory $ eavCustomerFactory ,
145
- \ Magento \ Customer \ Model \ ResourceModel \ Address \ CollectionFactory $ addressColFactory ,
155
+ CustomerFactory $ eavCustomerFactory ,
156
+ CollectionFactory $ addressColFactory ,
146
157
array $ data = []
147
158
) {
148
159
parent ::__construct (
@@ -178,19 +189,20 @@ public function __construct(
178
189
*/
179
190
protected function _initCustomers ()
180
191
{
181
- if (empty ($ this ->_customers )) {
192
+ if ($ this ->_customers === null ) {
193
+ $ this ->_customers = [];
182
194
// add customer default addresses column name to customer attribute mapping array
183
195
$ this ->_customerCollection ->addAttributeToSelect (self ::$ _defaultAddressAttributeMapping );
184
196
// filter customer collection
185
197
$ this ->_customerCollection = $ this ->_customerEntity ->filterEntityCollection ($ this ->_customerCollection );
186
198
187
- $ customers = [];
188
- $ addCustomer = function (\Magento \Customer \Model \Customer $ customer ) use (&$ customers ) {
189
- $ customers [$ customer ->getId ()] = $ customer ->getData ();
190
- };
199
+ $ selectIds = $ this ->_customerCollection ->getAllIdsSql ();
200
+ $ this ->_customerCollection ->setPageSize ($ this ->_pageSize );
201
+ $ pageCount = $ this ->_customerCollection ->getLastPageNumber ();
191
202
192
- $ this ->_byPagesIterator ->iterate ($ this ->_customerCollection , $ this ->_pageSize , [$ addCustomer ]);
193
- $ this ->_customers = $ customers ;
203
+ for ($ pageNum = 1 ; $ pageNum <= $ pageCount ; $ pageNum ++) {
204
+ $ this ->_customers += $ this ->loadCustomerData ($ selectIds , $ pageNum );
205
+ }
194
206
}
195
207
196
208
return $ this ;
@@ -211,7 +223,7 @@ protected function _getHeaderColumns()
211
223
/**
212
224
* Get customers collection
213
225
*
214
- * @return \Magento\Customer\Model\ResourceModel\Address\ Collection
226
+ * @return Collection
215
227
*/
216
228
protected function _getEntityCollection ()
217
229
{
@@ -227,7 +239,7 @@ public function export()
227
239
{
228
240
// skip and filter by customer address attributes
229
241
$ this ->_prepareEntityCollection ($ this ->_getEntityCollection ());
230
- $ this ->_getEntityCollection ()->setCustomerFilter (array_keys ($ this ->_customers ));
242
+ $ this ->_getEntityCollection ()->setCustomerFilter (array_keys ($ this ->getCustomers () ));
231
243
232
244
// prepare headers
233
245
$ this ->getWriter ()->setHeaderCols ($ this ->_getHeaderColumns ());
@@ -248,7 +260,7 @@ public function exportItem($item)
248
260
$ row = $ this ->_addAttributeValuesToRow ($ item );
249
261
250
262
/** @var $customer \Magento\Customer\Model\Customer */
251
- $ customer = $ this ->_customers [$ item ->getParentId ()];
263
+ $ customer = $ this ->getCustomers () [$ item ->getParentId ()];
252
264
253
265
// Fill row with default address attributes values
254
266
foreach (self ::$ _defaultAddressAttributeMapping as $ columnName => $ attributeCode ) {
@@ -274,10 +286,8 @@ public function exportItem($item)
274
286
*/
275
287
public function setParameters (array $ parameters )
276
288
{
277
- // push filters from post into export customer model
289
+ // push filters from post into export customer model
278
290
$ this ->_customerEntity ->setParameters ($ parameters );
279
- $ this ->_initCustomers ();
280
-
281
291
return parent ::setParameters ($ parameters );
282
292
}
283
293
@@ -290,4 +300,39 @@ public function getEntityTypeCode()
290
300
{
291
301
return $ this ->getAttributeCollection ()->getEntityTypeCode ();
292
302
}
303
+
304
+ /**
305
+ * Get Customers Data
306
+ *
307
+ * @return array
308
+ */
309
+ private function getCustomers (): array
310
+ {
311
+ $ this ->_initCustomers ();
312
+ return $ this ->_customers ;
313
+ }
314
+
315
+ /**
316
+ * Load Customers Data
317
+ *
318
+ * @param Select $selectIds
319
+ * @param int $pageNum
320
+ * @return array
321
+ */
322
+ private function loadCustomerData (Select $ selectIds , int $ pageNum = 0 ): array
323
+ {
324
+ $ select = $ this ->_customerCollection ->getConnection ()->select ();
325
+ $ select ->from (
326
+ ['customer ' => $ this ->_customerCollection ->getTable ('customer_entity ' )],
327
+ ['entity_id ' , 'email ' , 'store_id ' , 'website_id ' , 'default_billing ' , 'default_shipping ' ]
328
+ )->where (
329
+ 'customer.entity_id IN (?) ' , $ selectIds
330
+ );
331
+
332
+ if ($ pageNum > 0 ) {
333
+ $ select ->limitPage ($ pageNum , $ this ->_pageSize );
334
+ }
335
+
336
+ return $ this ->_customerCollection ->getConnection ()->fetchAssoc ($ select );
337
+ }
293
338
}
0 commit comments