2

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.???
});
Alex Gusev
  • 2,009
  • 3
  • 26
  • 45
  • Check https://magento.stackexchange.com/questions/203951/magento2-check-if-customer-is-logged-in-or-not-in-knockout-template . – amitshree Aug 15 '18 at 11:30

1 Answers1

3

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

customerData.get('customer')
Ben Crook
  • 15,685
  • 3
  • 52
  • 104
  • 1
    Does not work every time: let customer = customerData.get('customer'); let firstName = customer().firstname; let isLoggedIn = (typeof firstName != "undefined"); 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 let customer = window.customerData; let isLoggedIn = (typeof customer.id != "undefined"); for now. – Alex Gusev Aug 16 '18 at 14:09
  • Hi @BenCrook, where can i get the list of properties which i can get from the customerData.get(??) – Sudhir Jun 18 '20 at 11:53
  • You could try logging it to see what data is available to you, I haven't tried this but it may work - console.log(customerData.get('customer')) – Ben Crook Jun 18 '20 at 13:24