Implement Smart Login Detection in Magento 2!
  • 20+ years of experience
  • Top 1% software engineering talent
  • 675+ digital transformation experts

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.

Rohan Khatri

wordpress , magento , laravel developer

Tech expert in software development with a focus on delivering innovative, high-performance solutions. Skilled in creating scalable applications that enhance user experience and drive business growth. Passionate about transforming ideas into impactful digital products.

AWS vs Azure vs Google Cloud: Which Platform Is Best?