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.
- isLoggedIn()
- getCustomerId()
- getCustomerGroupId()
- getCustomer()
- getCustomerData()
We can call this methods as shown below.
1 2 3 4 5 6 | if($customerSession->isLoggedIn()) { customerSession->getCustomerId(); customerSession->getCustomerGroupId(); customerSession->getCustomer(); customerSession->getCustomerData(); } |
You can use above code snippet to get current logged in customer details. That’s it. Enjoy!!
Write an article about ecommerce that help people to grow their ecommerce business. You’ll find best ecommerce guide, news, tips & more!
April 30, 2020 at 4:20 pm
This article is really very interesting and effective. Keep it up!
May 7, 2020 at 3:00 pm
I learned a lot from it… Thanks, team.