Skip to content
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

Fix Customer custom attributes lost after save #17968

Merged
merged 3 commits into from
Oct 12, 2018
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
3 changes: 0 additions & 3 deletions app/code/Magento/Customer/Model/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,6 @@ public function updateData(AddressInterface $address)
// Need to explicitly set this due to discrepancy in the keys between model and data object
$this->setIsDefaultBilling($address->isDefaultBilling());
$this->setIsDefaultShipping($address->isDefaultShipping());
if (!$this->getAttributeSetId()) {
$this->setAttributeSetId(AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS);
}
$customAttributes = $address->getCustomAttributes();
if ($customAttributes !== null) {
foreach ($customAttributes as $attribute) {
Expand Down
17 changes: 10 additions & 7 deletions app/code/Magento/Customer/Model/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,13 +358,6 @@ public function updateData($customer)
$this->setId($customerId);
}

// Need to use attribute set or future updates can cause data loss
if (!$this->getAttributeSetId()) {
$this->setAttributeSetId(
CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER
);
}

return $this;
}

Expand Down Expand Up @@ -960,6 +953,16 @@ public function getSharedWebsiteIds()
return $ids;
}

/**
* Retrieve attribute set id for customer.
*
* @return int
*/
public function getAttributeSetId()
{
return parent::getAttributeSetId() ?: CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER;
}

/**
* Set store to customer
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,6 @@ public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $pa
$customerModel->setStoreId($this->storeManager->getStore()->getId());
}

// Need to use attribute set or future updates can cause data loss
if (!$customerModel->getAttributeSetId()) {
$customerModel->setAttributeSetId(
\Magento\Customer\Api\CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER
);
}
$this->populateCustomerWithSecureData($customerModel, $passwordHash);

// If customer email was changed, reset RpToken info
Expand Down
2 changes: 0 additions & 2 deletions app/code/Magento/Customer/Test/Unit/Model/CustomerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,6 @@ public function testUpdateData()
}

$expectedResult[$attribute->getAttributeCode()] = $attribute->getValue();
$expectedResult['attribute_set_id'] =
\Magento\Customer\Api\CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER;

$this->assertEquals($this->_model->getData(), $expectedResult);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,12 +334,6 @@ public function testSave()
$customerModel->expects($this->once())
->method('setId')
->with($customerId);
$customerModel->expects($this->once())
->method('getAttributeSetId')
->willReturn(null);
$customerModel->expects($this->once())
->method('setAttributeSetId')
->with(\Magento\Customer\Api\CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER);
$customerAttributesMetaData->expects($this->atLeastOnce())
->method('getId')
->willReturn($customerId);
Expand Down Expand Up @@ -616,12 +610,6 @@ public function testSaveWithPasswordHash()
$customerModel->expects($this->once())
->method('setId')
->with($customerId);
$customerModel->expects($this->once())
->method('getAttributeSetId')
->willReturn(null);
$customerModel->expects($this->once())
->method('setAttributeSetId')
->with(\Magento\Customer\Api\CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER);
$customerModel->expects($this->atLeastOnce())
->method('getId')
->willReturn($customerId);
Expand Down