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

  • Nidhi Arora
  • 4 years
  • 434 views
HOW TO CHECK IF CUSTOMER IS LOGGED IN OR NOT in MAGENTO 2
image_pdfPrintimage_printSave as PDF

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!

Join us for a FREE webinar