55 */
66namespace Magento \CustomerImportExport \Model \ResourceModel \Import \Customer ;
77
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 ;
910use Magento \Framework \DataObject ;
1011use 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 ;
1412use Magento \ImportExport \Model \ResourceModel \CollectionByPagesIterator ;
13+ use Magento \ImportExport \Model \ResourceModel \CollectionByPagesIteratorFactory ;
1514
1615/**
1716 * Storage to check existing customers.
@@ -56,6 +55,20 @@ class Storage
5655 */
5756 public $ _customerCollection ;
5857
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+
5972 /**
6073 * @param CustomerCollectionFactory $collectionFactory
6174 * @param CollectionByPagesIteratorFactory $colIteratorFactory
@@ -91,7 +104,7 @@ private function prepareCollection(array $customerIdentifiers): CustomerCollecti
91104 $ select = $ collection ->getSelect ();
92105 $ customerTableId = array_keys ($ select ->getPart (Select::FROM ))[0 ];
93106 $ select ->where (
94- $ customerTableId .'.email in (?) ' ,
107+ $ customerTableId . '.email in (?) ' ,
95108 array_map (
96109 function (array $ customer ) {
97110 return $ customer ['email ' ];
@@ -127,11 +140,15 @@ private function loadCustomersData(array $customerIdentifiers)
127140 */
128141 public function addCustomerByArray (array $ customer ): Storage
129142 {
130- $ email = strtolower (trim ($ customer ['email ' ]));
143+ $ email = mb_strtolower (trim ($ customer ['email ' ]));
131144 if (!isset ($ this ->_customerIds [$ email ])) {
132145 $ this ->_customerIds [$ email ] = [];
133146 }
147+ if (!isset ($ this ->customerStoreIds [$ email ])) {
148+ $ this ->customerStoreIds [$ email ] = [];
149+ }
134150 $ this ->_customerIds [$ email ][$ customer ['website_id ' ]] = $ customer ['entity_id ' ];
151+ $ this ->customerStoreIds [$ email ][$ customer ['website_id ' ]] = $ customer ['store_id ' ] ?? null ;
135152
136153 return $ this ;
137154 }
@@ -164,11 +181,7 @@ public function addCustomer(DataObject $customer): Storage
164181 public function getCustomerId (string $ email , int $ websiteId )
165182 {
166183 $ 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 );
172185
173186 if (isset ($ this ->_customerIds [$ email ][$ websiteId ])) {
174187 return $ this ->_customerIds [$ email ][$ websiteId ];
@@ -177,6 +190,25 @@ public function getCustomerId(string $email, int $websiteId)
177190 return false ;
178191 }
179192
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+
180212 /**
181213 * Pre-load customers for future checks.
182214 *
@@ -189,21 +221,21 @@ public function prepareCustomers(array $customersToFind): void
189221 foreach ($ customersToFind as $ customerToFind ) {
190222 $ email = mb_strtolower ($ customerToFind ['email ' ]);
191223 $ 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 )) {
195225 //Only looking for customers we don't already have ID for.
196226 //We need unique identifiers.
197- $ uniqueKey = $ email .'_ ' .$ websiteId ;
227+ $ uniqueKey = $ email . '_ ' . $ websiteId ;
198228 $ identifiers [$ uniqueKey ] = [
199229 'email ' => $ email ,
200230 'website_id ' => $ websiteId ,
201231 ];
202232 //Recording that we've searched for a customer.
203233 if (!array_key_exists ($ email , $ this ->_customerIds )) {
204234 $ this ->_customerIds [$ email ] = [];
235+ $ this ->customerStoreIds [$ email ] = [];
205236 }
206237 $ this ->_customerIds [$ email ][$ websiteId ] = null ;
238+ $ this ->customerStoreIds [$ email ][$ websiteId ] = null ;
207239 }
208240 }
209241 if (!$ identifiers ) {
@@ -213,4 +245,31 @@ public function prepareCustomers(array $customersToFind): void
213245 //Loading customers data.
214246 $ this ->loadCustomersData ($ identifiers );
215247 }
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+ }
216275}
0 commit comments