Customer custom attributes not saved when associated to other website then default website
Preconditions and environment
- Magento Commerce version 2.4.7-p3
- Magento extension-b2b
- PHP 8.3
Steps to reproduce
Step 1: Create 2 websites with a store Step 2: Create a customer attribute Step 3: Go to the customers -> all customers -> edit a customer Step 4: Associated the customer to the non default store Step 5: Save customer Step 6: Enter a value in the custom attribute (text field) Step 7: Save customer (initially attriibute is saved correct Step 8: Modify value in the custom attribute (text field) Step 9: Save customer (attribute is not saved)
As a workaround:
Step 1: Now go associate the customer to the default website Step 2: Save customer Step 3: Add value in the custom attribute Step 4: Save customer Step 5: Associate the customer to the non default website. Step 6: Save customer
Expected result
When a custom customer attribute is modified in admin customer edit it should be saved when associated to the non default website.
Actual result
When a custom customer attribute is modified in admin customer edit it is not saved when associated to the non default website.
Additional information
No response
Release note
No response
Triage and priority
- [ ] Severity: S0 - Affects critical data or functionality and leaves users without workaround.
- [ ] Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
- [x] Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
- [ ] Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
- [ ] Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.
Hi @rofokken. Thank you for your report. To speed up processing of this issue, make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce.
- For more details, review the Magento Contributor Assistant documentation.
- Add a comment to assign the issue:
@magento I am working on this - To learn more about issue processing workflow, refer to the Code Contributions.
Join Magento Community Engineering Slack and ask your questions in #github channel. :warning: According to the Magento Contribution requirements, all issues must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting. :clock10: You can find the schedule on the Magento Community Calendar page. :telephone_receiver: The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.
Hi @engcom-Bravo. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:
- [ ] 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
- [ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue.
- [ ] 3. Add
Area: XXXXXlabel to the ticket, indicating the functional areas it may be related to. - [ ] 4. Verify that the issue is reproducible on
2.4-developbranchDetails
- If the issue is reproducible on2.4-developbranch, please, add the labelReproduced on 2.4.x.
- If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here! - [ ] 5. Add label
Issue: Confirmedonce verification is complete. - [ ] 6. Make sure that automatic system confirms that report has been added to the backlog.
Hi @rofokken,
Thanks for your reporting and collaboration.
We have tried to reproduce the issue in Latest 2.4-develop with EE edition and we are not able to reproduce the issue.Kindly refer the attached video.
https://github.com/user-attachments/assets/345ad7c2-dcd2-42a0-a0b4-f9a17f7a3595
We are able to save the customer when we assign to the non default website.
Kindly recheck the issue in Latest 2.4-develop instance with EE edition and elaborate the steps to reproduce if the issue is still reproducible.
Thanks.
Hi Venkata Kumar Kuntumalla,
In your video i cannot see your confirmed your second save. You changed it to newone. Did it resulted in a actual save and is the Test attribute now "Newone"? I am missing "step 9". Initial save works, save after this one does not.
Thanks
@engcom-Bravo see also https://github.com/magento/magento2/issues/39591 same issue
Hi @rofokken,
Thanks for your update.
We have tried to reproduce the issue in Latest 2.4-develop instance with EE edition and the issue is reproducible.Kindly refer the screenshots.
When a custom customer attribute is modified in admin customer edit it is not saved when associated to the non default website.
Hence Confirming the issue.
Thanks.
:white_check_mark: Jira issue https://jira.corp.adobe.com/browse/AC-13886 is successfully created for this GitHub issue.
:white_check_mark: Confirmed by @engcom-Bravo. Thank you for verifying the issue.
Issue Available: @engcom-Bravo, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.
Any updates on this issue ?
Digging into it, I found the issue seems to appear when the model is being loaded by the interceptor itself
/**
* Load customer by email
*
* @param string $customerEmail
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function loadByEmail($customerEmail)
{
dump(get_class($this->_getResource()));
dump($this->getData());
$this->_getResource()->loadByEmail($this, $customerEmail);
dd($this->getData());
return $this;
}
If you take a deeper look and compare the behavior beetween, default store and custom store. You will see that the attribute is properly loaded as 1 in the default store...but not on the specific one
Default
"Magento\Customer\Model\ResourceModel\Customer\Interceptor"
array:1 [▼
"website_id" => "1"
]
array:33 [▼
"website_id" => "1"
"entity_id" => "15"
....
"activate_lcr" => "1"
]
Custom
"Magento\Customer\Model\ResourceModel\Customer\Interceptor"
array:1 [▼
"website_id" => "1"
]
array:33 [▼
"website_id" => "1"
"entity_id" => "15"
....
"activate_lcr" => "0" ====> Here we should have 1
]
So it appears the issue is more on the loading part rather than the saving part.
Any quickfix better than raw sql insert to handle this ?
I identified the issue stemming from the use of customerRepository->getById being called earlier within Save::_extractCustomerData. This call occurs in the default scope, which causes all attribute models to be loaded and cached in \Magento\Eav\Model\Config::getAttribute with $websiteId = 1.
However, during the actual save process, the current store is switched to the customer’s store (see Save.php:376). As a result, Magento initializes a different set of attribute model objects, which do not contain the previously loaded attribute data. This causes a fail to recognize any attribute changes during the upgrade process in vendor/magento/module-eav/Model/Entity/AbstractEntity.php:1362.
My workaround (likely a proper fix) is simply set the current store as the customer's store before _extractCustomerData
$this->storeManager->setCurrentStore($this->getCustomerStoreId());
// optional fields might be set in request for future processing by observers in other modules
$customerData = $this->_extractCustomerData();
private function getCustomerStoreId(): int
{
$storeId = null;
$originalRequestData = $this->getRequest()->getPostValue(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER);
$websiteId = isset($originalRequestData['website_id'])
? $originalRequestData['website_id']
: null;
if ($websiteId) {
$storeId = $this->storeManager->getWebsite($websiteId)?->getDefaultStore()->getId();
}
return (int) ($storeId ?? $this->storeManager->getStore()?->getStoreId());
}
Is this the same issue as https://github.com/magento/magento2/issues/38730?
We just upgraded to 2.4.7 and found this issue. I've patched our instance using the recommended solution above. Hope this helps others out there that are noticing this issue.
diff --git a/Controller/Adminhtml/Index/Save.php b/Controller/Adminhtml/Index/Save.php
--- a/Controller/Adminhtml/Index/Save.php
+++ b/Controller/Adminhtml/Index/Save.php
@@ -333,6 +333,7 @@
if ($this->getRequest()->getPostValue()) {
try {
+ $this->storeManager->setCurrentStore($this->getCustomerStoreId());
// optional fields might be set in request for future processing by observers in other modules
$customerData = $this->_extractCustomerData();
@@ -452,6 +453,28 @@
return $resultRedirect;
}
+ /**
+ * NPUDTC-9813 Some customer attributes not saving from the admin
+ * Related to github issue https://github.com/magento/magento2/issues/39587
+ * @return int
+ * @throws LocalizedException
+ * @throws NoSuchEntityException
+ */
+ private function getCustomerStoreId(): int
+ {
+ $storeId = null;
+ $originalRequestData = $this->getRequest()->getPostValue(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER);
+ $websiteId = isset($originalRequestData['website_id'])
+ ? $originalRequestData['website_id']
+ : null;
+
+ if ($websiteId) {
+ $storeId = $this->storeManager->getWebsite($websiteId)?->getDefaultStore()->getId();
+ }
+
+ return (int) ($storeId ?? $this->storeManager->getStore()?->getStoreId());
+ }
+
/**
* Update customer website subscriptions
*
Hi @rofokken,
Adobe Commerce Engineering team started working on this issue. We will reach out to you if we need more information and you will get notified once the issue is fixed. Please leave comments for any further questions. Thank you
Hi @rofokken,
Thank you for your valuable contribution. The Adobe Commerce Engineering team has attempted to reproduce the issue on the latest 2.4-develop branch following the steps provided, but was unable to replicate the issue - Customer custom attributes not saved when associated to other website. Attaching video for reference. Any additional reproduction details would be helpful.
https://github.com/user-attachments/assets/90a07d10-3075-4c8c-8cc1-e127d8c6a09f