How To

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

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!

Click to rate this post!
[Total: 2 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

Magento 2: Add Quantity Increment and Decrement on Category Page

Hello Magento Friends, In this blog, we will discuss about adding quantity increment and decrement…

1 day ago

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

4 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

6 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

6 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

1 week ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

1 week ago