5
5
*/
6
6
namespace Magento \CustomerImportExport \Model \ResourceModel \Import \Customer ;
7
7
8
- use Magento \CustomerImportExport \Test \Unit \Model \Import \CustomerCompositeTest ;
8
+ use Magento \Customer \Model \ResourceModel \Customer \Collection as CustomerCollection ;
9
+ use Magento \Customer \Model \ResourceModel \Customer \CollectionFactory as CustomerCollectionFactory ;
9
10
use Magento \Framework \DataObject ;
10
11
use Magento \Framework \DB \Select ;
11
- use Magento \Customer \Model \ResourceModel \Customer \CollectionFactory as CustomerCollectionFactory ;
12
- use Magento \Customer \Model \ResourceModel \Customer \Collection as CustomerCollection ;
13
- use Magento \ImportExport \Model \ResourceModel \CollectionByPagesIteratorFactory ;
14
12
use Magento \ImportExport \Model \ResourceModel \CollectionByPagesIterator ;
13
+ use Magento \ImportExport \Model \ResourceModel \CollectionByPagesIteratorFactory ;
15
14
16
15
/**
17
16
* Storage to check existing customers.
@@ -56,6 +55,20 @@ class Storage
56
55
*/
57
56
public $ _customerCollection ;
58
57
58
+ /**
59
+ * Existing customers store IDs. In form of:
60
+ *
61
+ * [customer email] => array(
62
+ * [website id 1] => store id 1,
63
+ * [website id 2] => store id 2,
64
+ * ... => ... ,
65
+ * [website id n] => store id n,
66
+ * )
67
+ *
68
+ * @var array
69
+ */
70
+ private $ customerStoreIds = [];
71
+
59
72
/**
60
73
* @param CustomerCollectionFactory $collectionFactory
61
74
* @param CollectionByPagesIteratorFactory $colIteratorFactory
@@ -91,7 +104,7 @@ private function prepareCollection(array $customerIdentifiers): CustomerCollecti
91
104
$ select = $ collection ->getSelect ();
92
105
$ customerTableId = array_keys ($ select ->getPart (Select::FROM ))[0 ];
93
106
$ select ->where (
94
- $ customerTableId .'.email in (?) ' ,
107
+ $ customerTableId . '.email in (?) ' ,
95
108
array_map (
96
109
function (array $ customer ) {
97
110
return $ customer ['email ' ];
@@ -127,11 +140,15 @@ private function loadCustomersData(array $customerIdentifiers)
127
140
*/
128
141
public function addCustomerByArray (array $ customer ): Storage
129
142
{
130
- $ email = strtolower (trim ($ customer ['email ' ]));
143
+ $ email = mb_strtolower (trim ($ customer ['email ' ]));
131
144
if (!isset ($ this ->_customerIds [$ email ])) {
132
145
$ this ->_customerIds [$ email ] = [];
133
146
}
147
+ if (!isset ($ this ->customerStoreIds [$ email ])) {
148
+ $ this ->customerStoreIds [$ email ] = [];
149
+ }
134
150
$ this ->_customerIds [$ email ][$ customer ['website_id ' ]] = $ customer ['entity_id ' ];
151
+ $ this ->customerStoreIds [$ email ][$ customer ['website_id ' ]] = $ customer ['store_id ' ] ?? null ;
135
152
136
153
return $ this ;
137
154
}
@@ -164,11 +181,7 @@ public function addCustomer(DataObject $customer): Storage
164
181
public function getCustomerId (string $ email , int $ websiteId )
165
182
{
166
183
$ email = mb_strtolower ($ email );
167
- //Trying to load the customer.
168
- if (!array_key_exists ($ email , $ this ->_customerIds ) || !array_key_exists ($ websiteId , $ this ->_customerIds [$ email ])
169
- ) {
170
- $ this ->loadCustomersData ([['email ' => $ email , 'website_id ' => $ websiteId ]]);
171
- }
184
+ $ this ->loadCustomerData ($ email , $ websiteId );
172
185
173
186
if (isset ($ this ->_customerIds [$ email ][$ websiteId ])) {
174
187
return $ this ->_customerIds [$ email ][$ websiteId ];
@@ -177,6 +190,25 @@ public function getCustomerId(string $email, int $websiteId)
177
190
return false ;
178
191
}
179
192
193
+ /**
194
+ * Find customer store ID for unique pair of email and website ID.
195
+ *
196
+ * @param string $email
197
+ * @param int $websiteId
198
+ * @return bool|int
199
+ */
200
+ public function getCustomerStoreId (string $ email , int $ websiteId )
201
+ {
202
+ $ email = mb_strtolower ($ email );
203
+ $ this ->loadCustomerData ($ email , $ websiteId );
204
+
205
+ if (isset ($ this ->customerStoreIds [$ email ][$ websiteId ])) {
206
+ return $ this ->customerStoreIds [$ email ][$ websiteId ];
207
+ }
208
+
209
+ return false ;
210
+ }
211
+
180
212
/**
181
213
* Pre-load customers for future checks.
182
214
*
@@ -189,21 +221,21 @@ public function prepareCustomers(array $customersToFind): void
189
221
foreach ($ customersToFind as $ customerToFind ) {
190
222
$ email = mb_strtolower ($ customerToFind ['email ' ]);
191
223
$ websiteId = $ customerToFind ['website_id ' ];
192
- if (!array_key_exists ($ email , $ this ->_customerIds )
193
- || !array_key_exists ($ websiteId , $ this ->_customerIds [$ email ])
194
- ) {
224
+ if (!$ this ->isLoadedCustomerData ($ email , $ websiteId )) {
195
225
//Only looking for customers we don't already have ID for.
196
226
//We need unique identifiers.
197
- $ uniqueKey = $ email .'_ ' .$ websiteId ;
227
+ $ uniqueKey = $ email . '_ ' . $ websiteId ;
198
228
$ identifiers [$ uniqueKey ] = [
199
229
'email ' => $ email ,
200
230
'website_id ' => $ websiteId ,
201
231
];
202
232
//Recording that we've searched for a customer.
203
233
if (!array_key_exists ($ email , $ this ->_customerIds )) {
204
234
$ this ->_customerIds [$ email ] = [];
235
+ $ this ->customerStoreIds [$ email ] = [];
205
236
}
206
237
$ this ->_customerIds [$ email ][$ websiteId ] = null ;
238
+ $ this ->customerStoreIds [$ email ][$ websiteId ] = null ;
207
239
}
208
240
}
209
241
if (!$ identifiers ) {
@@ -213,4 +245,31 @@ public function prepareCustomers(array $customersToFind): void
213
245
//Loading customers data.
214
246
$ this ->loadCustomersData ($ identifiers );
215
247
}
248
+
249
+ /**
250
+ * Load customer data if it's not loaded.
251
+ *
252
+ * @param string $email
253
+ * @param int $websiteId
254
+ * @return void
255
+ */
256
+ private function loadCustomerData (string $ email , int $ websiteId ): void
257
+ {
258
+ if (!$ this ->isLoadedCustomerData ($ email , $ websiteId )) {
259
+ $ this ->loadCustomersData ([['email ' => $ email , 'website_id ' => $ websiteId ]]);
260
+ }
261
+ }
262
+
263
+ /**
264
+ * Check if customer data is loaded
265
+ *
266
+ * @param string $email
267
+ * @param int $websiteId
268
+ * @return bool
269
+ */
270
+ private function isLoadedCustomerData (string $ email , int $ websiteId ): bool
271
+ {
272
+ return array_key_exists ($ email , $ this ->_customerIds )
273
+ && array_key_exists ($ websiteId , $ this ->_customerIds [$ email ]);
274
+ }
216
275
}
0 commit comments