1

I need to get customer status in JS component - authenticated or anonymous. Could I get it using Magento_Customer/js/customer-data and how?

define([
    'Magento_Customer/js/customer-data'
], function (customerData) {
    let isAnon = customerData.???
});
1

Yes, use customerData.get(property) where property is what you want to get. Example:

customerData.get('customer')
  • Does not work every time: <code> let customer = customerData.get('customer'); let firstName = customer().firstname; let isLoggedIn = (typeof firstName != "undefined"); </code> Sometimes firstname is undefined for logged in user. – Alex Gusev Aug 16 '18 at 12:06
  • What happens if you add domReady! as a dependency? Sounds like your script is loading too early. – Ben Crook Aug 16 '18 at 12:11
  • with domReady! result the same - does not work all times. I use this <code> let customer = window.customerData; let isLoggedIn = (typeof customer.id != "undefined"); </code> for now. – Alex Gusev Aug 16 '18 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.