Get Logged in Customer Data in Magento 2

As a Magento developer, whenever you are developing an online store or creating a module, getting a logged in customer data is a common work. So whenever you intend to get currently logged in customer data in Magento 2, this blog will help you.

In the below example, I will be using both Dependency Injection (DI) and Object Manager to get currently logged in customer data in Magento 2. However, Magento recommends using the dependency injection method to retrieve the logged in customer information. Whether you are developing an extension or a theme, avoid using the object manager method as you may receive technical rejection while submitting your extension to the Magento marketplace.

Using Dependency Injection (DI)

Here is the example code to get the logged in customer data in Magento 2 using dependency injection.

In order to get the logged in customer session data in Magento 2, we might need to inject the object of \Magento\Customer\Model\SessionFactory class in the constructor of our module’s block class and access it from the view ( .phtml ) file.

Sample File Path: app/code/YourCompanyName/YourModuleName/Block/YourCustomBlock.php

<?php
namespace YourCompanyName\YourModuleName\Block;
class YourCustomBlock extends \Magento\Framework\View\Element\Template
{
    protected $_customerSession;
        
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Customer\Model\SessionFactory $customerSession,
        array $data = []
    ) {
        $this->_customerSession = $customerSession->create();
        parent::__construct($context, $data);
    }
    
    public function getLoggedinCustomerId() {
        if ($this->_customerSession->isLoggedIn()) {
            return $this->_customerSession->getId();
        }
        return false;
    }

    public function getCustomerData() {
        if ($this->_customerSession->isLoggedIn()) {
            return $this->_customerSession->getCustomerData();
        }
        return false;
    }
}

Now, we can use the functions in our view (.phtml) file as follows.

$customerId = $block->getLoggedinCustomerId();
echo 'Customer Id: ' . $customerId;

$customerData = $block->getCustomerData();
if($customerData) {
    echo 'Customer Id: ' . $customerData->getId() . '<br/>';
    echo 'Customer Name: ' . $customerData->getName() . '<br/>';
    echo 'Customer Email: ' . $customerData->getEmail() . '<br/>';
    echo 'Customer Group Id: ' . $customerData->getGroupId() . '<br/>';
}

Using Object Manager

Here is the example code to get the logged in customer data in Magento 2 using object manager.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->create('Magento\Customer\Model\Session');
if ($customerSession->isLoggedIn()) {
    echo 'Customer Id: ' . $customerSession->getCustomer()->getId() . '<br/>';
    echo 'Customer Name: ' . $customerSession->getCustomer()->getName() . '<br/>';
    echo 'Customer Email: ' . $customerSession->getCustomer()->getEmail() . '<br/>';
    echo 'Customer Group Id: ' .  $customerSession->getCustomer()->getGroupId() . '<br/>';
}

Read: How to Get Customer Information By Id in Magento 2?

Hope this helps.

2 Comments on “Get Logged in Customer Data in Magento 2”

  1. Hey There!

    This all looks great, but creating my own code becoming a module, what else would I need instead of just the php file?

  2. Way to GO…Really Amazing Thanks for the Code Samples…
    Expecting a lot from u..Good Luck..Keep Going..

Leave a Reply

Your email address will not be published. Required fields are marked *