In Magento 2 we can check Customer is logged in or not by checking the given article,
Using the Block file, Pass Magento\Framework\App\Http\Context as a dependency to construct the method.
<?php namespace Rbj\Customer\Block; class Customerinfo extends \Magento\Framework\View\Element\Template { public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Framework\App\Http\Context $httpContext, array $data = [] ) { $this->httpContext = $httpContext; parent::__construct($context, $data); } /* * return bool */ public function getLogin() { return $this->httpContext->isLoggedIn(); } ?>
Call getLogin function in the template file,
$isLoggedin = $block->getLogin(); if($isLoggedin) { // show your message }
Never use Object Manager directly in real code in Magento 2.
Here I have shown just a demonstration using Objectmanager. Using Objectmanager directly create performance issue for the site.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $userContext = $objectManager->get('Magento\Framework\App\Http\Context'); $isLoggedIn = $userContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH); if($isLoggedIn) { // do coding for customer loggin }
First of all thanks for this great post. Yes using object manager directly in the code is a bad practice. However, it will make your guide more worthy if you will show some example and the output of the code implementation.
Hi Syed Muneeb Ul Hasan, You can get Customer session by below way also, But using below way, When you use varnish or any other third party Caching system, Sometimes In homepage you cant get current session by below way.so best way to get check customer login or not using \Magento\Framework\App\Http\Context $httpContext way.
public function __construct(
\Magento\Customer\Model\Session $customerSessiony to
) {
$this->session = $customerSession;
}
check using,
$isLoggedIn = $this->session->isLoggedIn()
Thanks for the response.
Yes, it’s a proper method. I am just suggesting to show some example in the guide.
Thanks for the valuable suggestion.
Can we use same way for product?
Can we use same way for product detail?
You can use above code at any page in site, It works.
Can we use same way for product detail?
Hi Syed Muneeb Ul Hasan, You can get Customer session by below way also, But using below way, When you use varnish or any other third party Caching system, Sometimes In homepage you cant get current session by below way.so best way to get check customer login or not using MagentoFrameworkAppHttpContext $httpContext way.
public function __construct(
MagentoCustomerModelSession $customerSessiony to
) {
$this->session = $customerSession;
}
check using,
$isLoggedIn = $this->session->isLoggedIn()
Thanks for the response.
Yes, it’s a proper method. I am just suggesting to show some example in the guide.
Thanks for the valuable suggestion.
Can we use same way for product?
You can use above code at any page in site, It works.
First of all thanks for this great post. Yes using object manager directly in the code is a bad practice. However, it will make your guide more worthy if you will show some example and the output of the code implementation.
Thank you for sharing it Rakesh!