Skip to content

#14663 Updating Customer through rest/all/V1/customers/:id resets group_id if group_id not passed in payload #25958

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Magento\Customer\Model\Data\CustomerSecureFactory;
use Magento\Customer\Model\Delegation\Data\NewOperation;
use Magento\Customer\Model\Delegation\Storage as DelegatedStorage;
use Magento\Customer\Model\ResourceModel\Customer\Collection;
use Magento\Framework\Api\DataObjectHelper;
use Magento\Framework\Api\ExtensibleDataObjectConverter;
use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
Expand All @@ -31,6 +32,8 @@
/**
* Customer repository.
*
* CRUD operations for customer entity
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.TooManyFields)
*/
Expand Down Expand Up @@ -187,8 +190,7 @@ public function save(CustomerInterface $customer, $passwordHash = null)
{
/** @var NewOperation|null $delegatedNewOperation */
$delegatedNewOperation = !$customer->getId() ? $this->delegatedStorage->consumeNewOperation() : null;
$prevCustomerData = null;
$prevCustomerDataArr = null;
$prevCustomerData = $prevCustomerDataArr = null;
if ($customer->getId()) {
$prevCustomerData = $this->getById($customer->getId());
$prevCustomerDataArr = $prevCustomerData->__toArray();
Expand All @@ -214,6 +216,7 @@ public function save(CustomerInterface $customer, $passwordHash = null)
$prevCustomerData ? $prevCustomerData->getStoreId() : $this->storeManager->getStore()->getId()
);
}
$this->setCustomerGroupId($customerModel, $customerArr, $prevCustomerDataArr);
// Need to use attribute set or future updates can cause data loss
if (!$customerModel->getAttributeSetId()) {
$customerModel->setAttributeSetId(CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER);
Expand Down Expand Up @@ -452,4 +455,18 @@ private function setValidationFlag($customerArray, $customerModel)
$customerModel->setData('ignore_validation_flag', true);
}
}

/**
* Set customer group id
*
* @param Customer $customerModel
* @param array $customerArr
* @param array $prevCustomerDataArr
*/
private function setCustomerGroupId($customerModel, $customerArr, $prevCustomerDataArr)
{
if (!isset($customerArr['group_id']) && $prevCustomerDataArr && isset($prevCustomerDataArr['group_id'])) {
$customerModel->setGroupId($prevCustomerDataArr['group_id']);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ public function testSave()
'setFirstFailure',
'setLockExpires',
'save',
'setGroupId'
]
);

Expand Down Expand Up @@ -245,9 +246,15 @@ public function testSave()
$this->customer->expects($this->atLeastOnce())
->method('getId')
->willReturn($customerId);
$this->customer->expects($this->atLeastOnce())
$this->customer->expects($this->at(4))
->method('__toArray')
->willReturn([]);
$this->customer->expects($this->at(3))
->method('__toArray')
->willReturn(['group_id' => 1]);
$customerModel->expects($this->once())
->method('setGroupId')
->with(1);
$this->customerRegistry->expects($this->atLeastOnce())
->method('retrieve')
->with($customerId)
Expand Down