How to check if customer is logged into Magento 2
In Magento 2, to check if customer is logged in, use the following code:
1 2 3 4 5 6 |
/** @var \Magento\Framework\App\ObjectManager $om */ $om = \Magento\Framework\App\ObjectManager::getInstance(); /** @var \Magento\Framework\App\Http\Context $context */ $context = $om->get('Magento\Framework\App\Http\Context'); /** @var bool $isLoggedIn */ $isLoggedIn = $context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH); |
Source (