Skip to content

Commit e076de2

Browse files
committed
MAGETWO-61907: [Backport] - Updating customer via REST API without address unsets default billing and default shipping address - for 2.1
1 parent 6547fab commit e076de2

File tree

1 file changed

+76
-25
lines changed

1 file changed

+76
-25
lines changed

app/code/Magento/Customer/Model/ResourceModel/CustomerRepository.php

Lines changed: 76 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ public function __construct(
130130
}
131131

132132
/**
133+
* Save.
134+
*
133135
* {@inheritdoc}
134136
*/
135137
public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $passwordHash = null)
@@ -175,19 +177,7 @@ public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $pa
175177
);
176178
}
177179
// Populate model with secure data
178-
if ($customer->getId()) {
179-
$customerSecure = $this->customerRegistry->retrieveSecureData($customer->getId());
180-
$customerModel->setRpToken($customerSecure->getRpToken());
181-
$customerModel->setRpTokenCreatedAt($customerSecure->getRpTokenCreatedAt());
182-
$customerModel->setPasswordHash($customerSecure->getPasswordHash());
183-
$customerModel->setFailuresNum($customerSecure->getFailuresNum());
184-
$customerModel->setFirstFailure($customerSecure->getFirstFailure());
185-
$customerModel->setLockExpires($customerSecure->getLockExpires());
186-
} else {
187-
if ($passwordHash) {
188-
$customerModel->setPasswordHash($passwordHash);
189-
}
190-
}
180+
$this->populateCustomerModelWithSecureData($customer, $passwordHash, $customerModel);
191181

192182
// If customer email was changed, reset RpToken info
193183
if ($prevCustomerData
@@ -197,19 +187,9 @@ public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $pa
197187
$customerModel->setRpTokenCreatedAt(null);
198188
}
199189

200-
if (!array_key_exists('default_billing', $customerArr) &&
201-
null !== $prevCustomerDataArr &&
202-
array_key_exists('default_billing', $prevCustomerDataArr)
203-
) {
204-
$customerModel->setDefaultBilling($prevCustomerDataArr['default_billing']);
205-
}
190+
$this->setDefaultBilling($customerArr, $prevCustomerDataArr, $customerModel);
206191

207-
if (!array_key_exists('default_shipping', $customerArr) &&
208-
null !== $prevCustomerDataArr &&
209-
array_key_exists('default_shipping', $prevCustomerDataArr)
210-
) {
211-
$customerModel->setDefaultShipping($prevCustomerDataArr['default_shipping']);
212-
}
192+
$this->setDefaultShipping($customerArr, $prevCustomerDataArr, $customerModel);
213193

214194
$customerModel->save();
215195
$this->customerRegistry->push($customerModel);
@@ -226,6 +206,8 @@ public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $pa
226206
}
227207

228208
/**
209+
* Get.
210+
*
229211
* {@inheritdoc}
230212
*/
231213
public function get($email, $websiteId = null)
@@ -235,6 +217,8 @@ public function get($email, $websiteId = null)
235217
}
236218

237219
/**
220+
* Get by Id.
221+
*
238222
* {@inheritdoc}
239223
*/
240224
public function getById($customerId)
@@ -244,6 +228,8 @@ public function getById($customerId)
244228
}
245229

246230
/**
231+
* Get list.
232+
*
247233
* {@inheritdoc}
248234
*/
249235
public function getList(SearchCriteriaInterface $searchCriteria)
@@ -296,6 +282,8 @@ public function getList(SearchCriteriaInterface $searchCriteria)
296282
}
297283

298284
/**
285+
* Delete.
286+
*
299287
* {@inheritdoc}
300288
*/
301289
public function delete(\Magento\Customer\Api\Data\CustomerInterface $customer)
@@ -304,6 +292,8 @@ public function delete(\Magento\Customer\Api\Data\CustomerInterface $customer)
304292
}
305293

306294
/**
295+
* Delete by Id.
296+
*
307297
* {@inheritdoc}
308298
*/
309299
public function deleteById($customerId)
@@ -373,4 +363,65 @@ private function updateAddresses(\Magento\Customer\Api\Data\CustomerInterface $c
373363
}
374364
}
375365
}
366+
367+
/**
368+
* Populate customer model with secure data.
369+
*
370+
* @param \Magento\Customer\Api\Data\CustomerInterface $customer
371+
* @param $passwordHash
372+
* @param $customerModel
373+
*/
374+
private function populateCustomerModelWithSecureData(
375+
\Magento\Customer\Api\Data\CustomerInterface $customer,
376+
$passwordHash,
377+
$customerModel
378+
) {
379+
if ($customer->getId()) {
380+
$customerSecure = $this->customerRegistry->retrieveSecureData($customer->getId());
381+
$customerModel->setRpToken($customerSecure->getRpToken());
382+
$customerModel->setRpTokenCreatedAt($customerSecure->getRpTokenCreatedAt());
383+
$customerModel->setPasswordHash($customerSecure->getPasswordHash());
384+
$customerModel->setFailuresNum($customerSecure->getFailuresNum());
385+
$customerModel->setFirstFailure($customerSecure->getFirstFailure());
386+
$customerModel->setLockExpires($customerSecure->getLockExpires());
387+
} else {
388+
if ($passwordHash) {
389+
$customerModel->setPasswordHash($passwordHash);
390+
}
391+
}
392+
}
393+
394+
/**
395+
* Set default billing.
396+
*
397+
* @param $customerArr
398+
* @param $prevCustomerDataArr
399+
* @param $customerModel
400+
*/
401+
private function setDefaultBilling($customerArr, $prevCustomerDataArr, $customerModel)
402+
{
403+
if (!array_key_exists('default_billing', $customerArr) &&
404+
null !== $prevCustomerDataArr &&
405+
array_key_exists('default_billing', $prevCustomerDataArr)
406+
) {
407+
$customerModel->setDefaultBilling($prevCustomerDataArr['default_billing']);
408+
}
409+
}
410+
411+
/**
412+
* Set default shipping.
413+
*
414+
* @param $customerArr
415+
* @param $prevCustomerDataArr
416+
* @param $customerModel
417+
*/
418+
private function setDefaultShipping($customerArr, $prevCustomerDataArr, $customerModel)
419+
{
420+
if (!array_key_exists('default_shipping', $customerArr) &&
421+
null !== $prevCustomerDataArr &&
422+
array_key_exists('default_shipping', $prevCustomerDataArr)
423+
) {
424+
$customerModel->setDefaultShipping($prevCustomerDataArr['default_shipping']);
425+
}
426+
}
376427
}

0 commit comments

Comments
 (0)