Magento 2.x – Validate if a customer is logged in or not


To check if customer is loged in or not you can call Magento\Customer\Model\Session::isLoggedIn() method.

If you want to do that properly in *.phtml files you should use helper inside the template, you will see a ton of examples using the ObjectManager, but you should always bear in mind that it’s not a good coding practice.

So, let’s perform the following steps:

  • Create YourCompany\ModuleName\Helper\Data
  • Use the previous Helper, in your phtml as follows:

     

Before trying, remember to re-compile, and clean cache.