Customer data is empty on the first page after login.
Related to closed issue #13984, with more steps to reproduce.
define([
'jquery',
'Magento_Customer/js/customer-data',
'domReady!'
], function ($, customerData) {
'use strict';
var customer = customerData.get('customer');
console.log(customer());
});
console.log(customer()); gives "{}" in consoleconsole.log(customer()); gives right infos in consoleHi @AymericJoubert. Thank you for your report.
To help us process this issue please make sure that you provided the following information:
Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:
@magento-engcom-team give me 2.3-develop instance - upcoming 2.3.x release
For more details, please, review the Magento Contributor Assistant documentation.
@AymericJoubert do you confirm that you was able to reproduce the issue on vanilla Magento instance following steps to reproduce?
@magento-engcom-team give me 2.3-develop instance
Hi @AymericJoubert. Thank you for your request. I'm working on Magento 2.3-develop instance for you
Hi @AymericJoubert, here is your Magento instance.
Admin access: https://i-21548-2-3-develop.instances.magento-community.engineering/admin
Login: admin Password: 123123q
Instance will be terminated in up to 3 hours.
Can't reproduce it without adding provided piece of javascript, and need sample datas
@magento-engcom-team Same or similar issue here on 2.2.7. In my case, it cases infinite loading of a product page due to failure of loading customer data.
@magento-engcom-team give me 2.2.7 instance with sample data and frontend access
Hi @Ctucker9233. Thank you for your request. I'm working on Magento 2.2.7 instance for you
Hi @Ctucker9233, here is your Magento instance.
Admin access: https://i-21548-2-2-7.instances.magento-community.engineering/admin
Login: admin Password: 123123q
Instance will be terminated in up to 3 hours.
So what's the roadmap about this issue ?
@AymericJoubert expected result is wrong, customer data is loaded asynchronously. You can add observable on it to perform some logic when customer data is loaded.
Try something like this:
customer.subscribe(function (_customer) {
console.log(_customer);
}, this);
subscription to customer data is not being executed when Local Storage has mage-cache-storage: {}
@AymericJoubert give a try to following patch
--- view/frontend/web/js/customer-data.js.org 2019-08-21 18:03:30.682111847 +0200
+++ view/frontend/web/js/customer-data.js 2019-08-21 18:08:12.738262086 +0200
@@ -202,10 +202,10 @@
expiredSectionNames = this.getExpiredSectionNames(),
isLoading = false;
- if (privateContent &&
+ if ((privateContent &&
!$.cookieStorage.isSet(privateContentVersion) &&
!$.localStorage.isSet(privateContentVersion)
- ) {
+ ) || (privateContent === null && localPrivateContent === null)) {
$.cookieStorage.set(privateContentVersion, needVersion);
$.localStorage.set(privateContentVersion, needVersion);
this.reload([], false);
I could reproduce this following way:
Hello. I'm a new programmer in Magento 2, but I want to give my contribution to the post. Maybe I'm not providing the best solution, but could help someone.
The data stored by the customerData js feature could be updated asynchronously and explicitly registering a subscriber solve the problem pointed by AymericJoubert. Using the code that he posted, add the following:
define([
'jquery',
'Magento_Customer/js/customer-data',
'domReady!'
], function ($, customerData) {
'use strict';
var customer = customerData.get('customer');
customer.subscribe(function (updatedCustomer)
{
console.log(updatedCustomer);
}, this);
});
So, that way, when the customer data is updated, our widget or component receive the updated data and could does our logic verifications. It solved the problem to me of not verifying correctly the customer data on the first page load after login, because that code run asynchronously when the data is updated.
Hope that helps.
Most helpful comment
Hello. I'm a new programmer in Magento 2, but I want to give my contribution to the post. Maybe I'm not providing the best solution, but could help someone.
The data stored by the customerData js feature could be updated asynchronously and explicitly registering a subscriber solve the problem pointed by AymericJoubert. Using the code that he posted, add the following:
So, that way, when the customer data is updated, our widget or component receive the updated data and could does our logic verifications. It solved the problem to me of not verifying correctly the customer data on the first page load after login, because that code run asynchronously when the data is updated.
Hope that helps.