magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

Customer custom attributes not saved when associated to other website then default website

Open rofokken opened this issue 11 months ago • 14 comments

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”.

rofokken avatar Jan 30 '25 08:01 rofokken

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.


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.

m2-assistant[bot] avatar Jan 30 '25 08:01 m2-assistant[bot]

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: XXXXX label to the ticket, indicating the functional areas it may be related to.
  • [ ] 4. Verify that the issue is reproducible on 2.4-develop branch
    Details- If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced 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: Confirmed once verification is complete.
  • [ ] 6. Make sure that automatic system confirms that report has been added to the backlog.

m2-assistant[bot] avatar Jan 30 '25 09:01 m2-assistant[bot]

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.

engcom-Bravo avatar Jan 31 '25 06:01 engcom-Bravo

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

rofokken avatar Jan 31 '25 10:01 rofokken

@engcom-Bravo see also https://github.com/magento/magento2/issues/39591 same issue

rofokken avatar Feb 03 '25 18:02 rofokken

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.

Image

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.

engcom-Bravo avatar Feb 05 '25 06:02 engcom-Bravo

:white_check_mark: Jira issue https://jira.corp.adobe.com/browse/AC-13886 is successfully created for this GitHub issue.

github-jira-sync-bot avatar Feb 05 '25 06:02 github-jira-sync-bot

: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.

m2-assistant[bot] avatar Feb 05 '25 06:02 m2-assistant[bot]

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 ?

IClongI avatar Apr 02 '25 10:04 IClongI

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());
}

longnt195 avatar May 19 '25 03:05 longnt195

Is this the same issue as https://github.com/magento/magento2/issues/38730?

rikwillems avatar May 22 '25 07:05 rikwillems

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
      *

ehalpin-deg avatar Oct 06 '25 02:10 ehalpin-deg

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

engcom-Bravo avatar Dec 11 '25 09:12 engcom-Bravo

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

engcom-Bravo avatar Dec 12 '25 09:12 engcom-Bravo