As we know, there are so many situation where we require to check whether customer is currently logged in or not. In this blog post you will learn about how to check customer is logged in or not in Magento 2. In order to check this situation you can use below code snippet.
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); |
Once you got the result in $isLoggedIn variable, you can check condition as per below.
1 2 3 4 5 6 | if($isLoggedIn){ // If customer is logged in // Some code snippet here } |
You can use above code snippet. That’s it. Enjoy!!
Write an article about ecommerce that help people to grow their ecommerce business. You’ll find best ecommerce guide, news, tips & more!
Leave a Reply