How to Check if Customer is Logged in or Not

 



<?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);

    }

?>





------------------------------------------------------------------------------------



<?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());

}




Comments

Popular posts from this blog

Add Admin User name and Action name in Order Comment Section through Action Performed from Sales Order Grid in Magento 2

How to Set and Get Cookie in Magento 2

How to Get Invoice Comments List Programmatically in Magento 2