This question already has an answer here:

After a customer is logged in, when I check if the customer is logged in or not, it returns true only on the customer account page and returns false on every other page like home page, product page, etc. This is the code which I am using to check

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

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

    <?php if($customerSession->isLoggedIn()) {
        echo "YEs logged in";


marked as duplicate by Piyush, Shoaib Munir, Jai, Mohit Kumar Arora, Manashvi Birla Aug 9 at 8:57

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


Note: You should not use an objectmanager directly into the code.

You first need to inject the following class in your constructor: /Magento/Customer/Model/Session

protected $_session;

public function __construct(
  \Magento\Customer\Model\Session $session,
) {
  $this->_session = $session;

Then in your function in the same class, you can call the following:

if ($this->_session->isLoggedIn()) {
// Customer is logged in 
} else {
// Customer is not logged in

Also you can create method which return true or false whether a customer is logged in or not:

public function isCustomerLoggedIn()
  return $this->_session->isLoggedIn();

You can call this method in your block's template file.

Not the answer you're looking for? Browse other questions tagged or ask your own question.