We can use the following code snippet to check whether the customer is logged in or not in Magento2 with the help of customer session model class object.
use Magento\Customer\Model\Session;
$protected customerSession;
/**
* Class constructor.
* @param Session $customerSession
*/
public function __construct(
Session $customerSession
)
{
$this->customerSession = $customerSession;
}
/**
* check whether customer is logged in or not
*
* @return bool
*/
public function isCustomerLoggedIn()
{
$this->customerSession->isLoggedIn() ? true : false;
}
Like this:
Like Loading...