How to Get Logged in Customer Data in Magento 2
Knowing your potential buyers is the key to selling them effectively. Collecting the data of logged in customers provides you the important details to know who your customers are, what they want, and how to serve them better. Magento 2 store admin may want to implement certain features based on customer attributes of logged in users. For example, only logged in customers are allowed to view the price details rather than that hide price for not logged in customers. Admin may want to offer shipping services to particular customer groups only.
Customer attributes like age, location, gender can be handy for marketing insights. For example, you can target young customers as per their taste. Women may obviously not want to get suggestions for men’s trousers!
Admin needs to retrieve customer attributes value for such features. To get logged in customer data in Magento 2, one needs to follow one of the below methods.
Methods to get logged in customer data in Magento 2:
- using Object Manager
1234$om = \Magento\Framework\App\ObjectManager::getInstance();$customerSession = $om->get('Magento\Customer\Model\Session');$customerData = $customerSession->getCustomer()->getData(); //get all data of customerData$customerData = $customerSession->getCustomer()->getId();//get id of customer - using Block class
123456789101112protected $customer;public function __construct(\Magento\Customer\Model\Session $customer){$this->customer = $customer;}public function yourMethodName(){$customer = $this->customer;$customerName = $customer->getName();$customerId = $customer->getId();//You will get all basic detail with this $customer object}
Related read: How to get logged in customer address in Magento 2?
Follow the above method and implement flawless features based on customer attributes!
Doubts regarding the topic are welcome in the comments section below and I’d be happy to help. ?
Flash 5 stars if the post has helped you to offer amazing features for your privileged customers ?
Happy Coding! ?
Still need help? Hire our Adobe-certified Magento experts.
Related Reads:
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
8 Comments
I’m not able to get customers sessions on all pages, it comes only on cart detail page.
I have created on controller to get logged in customers session data. but i’m getting that data only on that controller and cart details page only.
I want that logged in customers data on all pages of website.
please let me know if you have any solution.
Thank you
Hello Pravin,
We are getting the required data with the same solution from our end.
Thank You
ParseError: syntax error, unexpected ‘$customerSession’ (T_VARIABLE) in /home/diasrl/catalogo.diasrl.it/app/code/PHPCuong/CustomerRedirecting/Controller/Account/LoginPost.php:236 Stack trace: #0 /home/diasrl/catalogo.diasrl.it/vendor/composer/ClassLoader.php(322):
getting error in login post controller
Hello,
There must be a mistake in your controller code.
Please check your controller file.
Thank You.
Hi
I tried through rest API but not working. Can you provide solution to show logged in customer through rest api.
Hello Neeraj,
The API is different from the token you get from REST API.
Thanks.
Block class way is not working on magento 2.4 right ?
Hello Chamika,
It is working in Magento 2.4.
Thank you.