Magento 2 – Get current logged in customer details

Magewallet | Magento 2 Extensions, Themes & Solutions

Magento 2 – Get current logged in customer details

In this blog post, we will see how to retrieve currently logged in customer details in Magento 2.

We can retrieve the currently logged in customer details using an instance of \Magento\Customer\Model\Session model through constructor. After initialization of model instant, we need to store instantiated object into protected variable like $customerSession. Session model contains so many methods related to customer and we can use this methods to retrieve customers details. Below are some of the methods.

  1. isLoggedIn()
  2. getCustomerId()
  3. getCustomerGroupId()
  4. getCustomer()
  5. getCustomerData()

We can call this methods as shown below.

You can use above code snippet to get current logged in customer details. That’s it. Enjoy!!

admin

3 Comments

Anthony Arnett

34 mins ago

helpful post...can you please share code to get admin user details or session in magento 2.

Momotenko

34 mins ago

Its not working after cache enabled.

admin

34 mins ago

Thanks for your feedback. You may find the answer here: http://magento.stackexchange.com/questions/86674/block-with-cachable-false-not-rendered-on-product-view-page/90130#90130 Or May be this case will be fix in next Magento 2.x version.

Leave Comment

Newsletter

Latest Tweets