Site icon MageComp Blog

Magento 2: How to Check if Customer is Logged in or Not?

How to Check if Customer is Logged in or Not M2

Hello Magento Friends,

In today’s tutorial, I will show How to Check if a Customer is Logged in or not in Magento 2.

Sometimes store owners want to treat logged-in and non-logged-in customers differently. Say, for example, you want to offer discounts or promotions only to customers who are logged in to your store. Or you want to allow only logged-in customers to access some functionality of your store and restrict it for not logged-in customers. In such cases, you need the login status of customers.

You can force customers to log in to your store to take full advantage of the benefits you offer to your logged-in customers.

Increase the number of customer logins for your Magento 2 store by providing them with various quick login options like Social Login, Mobile Number Login, Google One Tap Login, and Ajax Popup Login to help them take maximum benefits from your store.

Here, I have explained two ways to check whether the customer is logged in or not in Magento 2.

Steps to Check if Customer is Logged in or Not in Magento 2:

We can check if the customer is logged in or not in Magento 2 using the below methods.

Let’s get started,

Method 1: Using Helper file

Create a helper file at the below path.

app/code/Vendor/Extension/Helper/Data.php

Then add the code as follows

<?php

namespace Vendor\Extension\Helper;

class Data 
{
    public function __construct(
       \Magento\Framework\App\Http\Context $httpContext
    ) {
        $this->httpContext = $httpContext;
    }

    public function getLogin() {
        return $this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
    }
?>

Note – Based on your requirement, you can use the getLogin() method in phtml, controller etc.

Method 2: Using Objectmanager

Create customer.php in your Magento root directory.

{{magento_root}}/customer.php

Now, add the following code

<?php 
 
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
 
try
{
    $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();
    $appState = $objectManager->get('\Magento\Framework\App\State');
    $appState->setAreaCode('frontend');
 
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $userContext = $objectManager->get('Magento\Framework\App\Http\Context');
    $loggedin = $userContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
   
    if($loggedin) {
      echo "Customer is Logged-In";
    }
}
catch(\Exception $e)
{
    print_r($e->getMessage());
}

Conclusion:

Hence, using the above methods, you can easily determine the customers that are logged in to your Magento 2 store. If you face any difficulty, share it with me through the comment section. Share the solution with your Magento friends and stay updated with us.

Happy Coding!

Exit mobile version