Mitech Preloader
Magento

How to Check, Customer is Logged in or not in Magento2

Below script will helpful to find the customer currently logged in a website or not.

[php]

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->create(‘Magento\Customer\Model\Session’);
if($customerSession->isLoggedIn()) { //customer is loggedin or not
$customerSession->getCustomer()->getName(); //Loggedin customer Name
$customerSession->getCustomer()->getEmail(); //Loggedin customer Email
//print_r($customerSession->getCustomer()->getData());
}

[/php]

Or You can try below code

[php]

$om = \Magento\Framework\App\ObjectManager::getInstance();
$context = $om->get(‘Magento\Framework\App\Http\Context’);
$isLoggedIn = $context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
if($isLoggedIn){
echo “Yes Customer loggedin”;
//print_r($context->getData());
}

[/php]

Hope the post helped you out! Don’t forget to share!

blank