How to check If Customer is Logged In or Not Magento 2?

  • Posted by Envision Ecommerce
  • /
  • March 1, 2017
HOW TO CHECK IF CUSTOMER IS LOGGED IN OR NOT in MAGENTO 2

Method 1. To check the customer is logged or not on any page of the website:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$customerSession = $objectManager->get('Magento\Customer\Model\Session');

if($customerSession->isLoggedIn()) {

   // customer login action

}

Method 2.  To check if customer is logged from controller:

$this->_objectManager->get('Magento\Customer\Model\Session');

if($customerSession->isLoggedIn()) {

   // customer login action

}

If you still have any query regarding this “How to” or would like to add some suggestions to this solution, let us know your feedback or query at [email protected], and don’t forget to share this “How to” blog with your fellow Magento 2 users!