🔥 Just Launched! Werra Premium Template for HyväSee it in Action

How to Get Logged in Customer Data in Magento 2

By Sanjay JethvaUpdated on May 22, 2025 2 min read

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:

1 .using Object Manager

$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

2.  using Block class

protected $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!

Happy Coding! ?

Sanjay Jethva Full Image
Article bySanjay 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.