Scroll to top
© Copyright 2023 StageBit. | All Rights Reserved
Share
  • November 18, 2019
  • 1 MIN READ

Check if Customer is Logged in or not in Magento 2

~Written By Mitali Kundale

Magento 2
 

As we all know, there are so many conditions where we need to check whether the customer is currently logged in or not for specific functionality. In this blog post, you will have an idea about How to check Magento 2 Customers login or not in Magento 2?

It is always best practice to don’t use ObjectManager directly. You can use the following code to check whether the user is logged in or not in any class except template files.

You need to use this Magento\Customer\Model\Session::isLoggedIn() method in following way.  First, you need to inject the following class in your constructor: /Magento/Customer/Model/Session.

protected $_session;

public function __construct(
    ...
    \Magento\Customer\Model\Session $session,
    ...
) {
    ...
    $this->_session = $session;
    ...
}

Then in your class, you can use the following snippet of code anywhere.

if ($this->_session->isLoggedIn()) {
    // Customer is logged in 
} else {
    // Customer is not logged in
}

This was how to check Magento 2 Customers login status. Hope you found it useful.

Related posts

Post a Comment

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