/** * Update customer model with the data from the data object * * @param Customer $customerModel * @param CustomerDataObject $customerData * @return void */ public function updateCustomerModel(\Magento\Customer\Model\Customer $customerModel, CustomerDataObject $customerData) { $attributes = ExtensibleDataObjectConverter::toFlatArray($customerData); foreach ($attributes as $attributeCode => $attributeValue) { $customerModel->setDataUsingMethod($attributeCode, $attributeValue); } $customerId = $customerData->getId(); if ($customerId) { $customerModel->setId($customerId); } // Need to use attribute set or future calls to customerModel::save can cause data loss if (!$customerModel->getAttributeSetId()) { $customerModel->setAttributeSetId(CustomerMetadataServiceInterface::ATTRIBUTE_SET_ID_CUSTOMER); } }