113 votes

How to check if customer is logged in or not?

Important reminder: One should never call the Object Manager directly Thus here's how to do it the clean way In any class except templates You first need to inject the following class in your ...
Raphael at Digital Pianism's user avatar
82 votes
Accepted

How to check if customer is logged in or not?

Following code you can check customer login or not anywhere $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $customerSession = $objectManager->get('Magento\Customer\Model\...
Sohel Rana's user avatar
  • 35.5k
44 votes

How to check if customer is logged in or not?

It is possible via Magento\Framework\App\Http\Context or via Magento\Customer\Model\Session. However, the result may be different: HTTP context is initialized earlier than the customer session (but ...
Alex Paliarush's user avatar
23 votes

How to set and get customer session data in magento 2

You can Set and get Customer session by using Magento\Customer\Model\Session protected $customerSession; public function __construct( \Magento\Customer\Model\Session $customerSession ){ $...
Krishna ijjada's user avatar
22 votes
Accepted

customer session does not work except customer page - Magento 2

To have access to real data in Magento 2 session you have to apply one of the following Do it on non-cacheable page. Disable full page cache. Set $this->$_isScopePrivate = true for block where ...
Sergii Ivashchenko's user avatar
19 votes

How to check if customer is logged in or not?

None of these solutions worked for me. Some pages would appear to be logged in but others would not. It seems this is the bug: https://github.com/magento/magento2/issues/3294 I ended up creating a ...
Carl Owens's user avatar
18 votes

Customer Session Not working in magento 2

I am facing same problem when Cache enable I am not able to get customer session.But I find below solution /** * @var \Magento\Customer\Model\Session */ protected $_customerSession; ...
Prashant Valanda's user avatar
17 votes

How to check if customer is logged in or not?

/** @var \Magento\Framework\App\ObjectManager $om */ $om = \Magento\Framework\App\ObjectManager::getInstance(); /** @var \Magento\Framework\App\Http\Context $context */ $context = $om->get('Magento\...
Dmitrii Fediuk's user avatar
14 votes

How to check if customer is logged in or not?

None of the solutions here worked for me reliably in Magento v2.1 with Full Page Cache and Varnish enabled in Production mode. I finally found a solution that worked 100% of the time with all caching ...
thdoan's user avatar
  • 728
13 votes

How to check if customer is logged in or not?

To get user logged in at template, you can simply call helper in just one single line : <?php $_loggedin = $this->helper('Magento\Checkout\Helper\Cart')->getCart()->getCustomerSession()-&...
Afham's user avatar
  • 366
13 votes

Why Customer Session is not working on full page cache?

You need to use the factory method when FPC is enabled. By Factory Method : /** * @var \Magento\Customer\Model\Session */ protected $_customerSessionFactory; public function __construct( .... ...
Rohan Hapani's user avatar
  • 17.3k
12 votes

How to check if customer is logged in or not?

Another answer: <?php $_loggedin = $block->getLayout()->createBlock('Magento\Customer\Block\Account\AuthorizationLink')->isLoggedIn() ?> <?php if( $_loggedin ) : ?> // your ...
xanka's user avatar
  • 2,084
10 votes

How to check if customer is logged in or not?

Fetching the logged status from Session model will not work in case you want to use it after enabling Magento default FPC cache, in that case, you should use SessionFactory instead. Session is not ...
Wolfack's user avatar
  • 281
10 votes

How to display customer name when logged in Magento 2?

There are two way Using object manager into your phtml file: $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $customerSession = $objectManager->create('Magento\Customer\...
Manish's user avatar
  • 3,096
9 votes

Magento 2.2.2: Customer session not working with cache enable

The best practice is to use Magento\Framework\App\Http\Context to check the customer logged in or not. And, avoiding using Object Manager directly. For example: <?php namespace Vendor\Customer\...
Khoa TruongDinh's user avatar
9 votes
Accepted

How to get CustomerId in API in magento 2? and how magento gets customerId for the same?

Finally got the solution, i am trying my best to explain it. so here's how Magento works !! Magento gets the customer data with passing anything except token value in API call. In between magento ...
Aditya Shah's user avatar
  • 7,517
8 votes

Magento 2 customer logged in checking - in \Magento\Customer\Model\Session method isLoggedIn() always returns false

If store cache is enable, you can not get customer session. Check below code for get customer session with enable cache. /** * @var \Magento\Customer\Model\Session */ protected $_customerSession; ...
Suresh Chikani's user avatar
8 votes
Accepted

Magento 2: get customer id of logged in user with cache enabled

I have updated my response. In my previous answer, I had a block cacheable=false. This is incorrect because it do the page is not cacheable. For knowing if the customer is logged or not, you must do ...
raumatbel's user avatar
  • 1,215
7 votes

How to check if customer is logged in or not?

There are a lot of answers out there that go something like this... GET OBJECT MANAGER LOAD UP CLASS MODEL DO STUFF This is the WRONG methodology to use in Magento2.0. In 2.0, the auto generated ...
CarComp's user avatar
  • 1,226
7 votes
Accepted

Magento 2.2.2: Customer session not working with cache enable

Use below code: $ObjectManager= \Magento\Framework\App\ObjectManager::getInstance(); $context = $ObjectManager->get('Magento\Framework\App\Http\Context'); $isLoggedIn = $context->getValue(\...
Kishan Patadia's user avatar
7 votes
Accepted

How to Add custom data to customer session?

You need to create customer_login event observer for set value in customer session after login 1) Create events.xml app/code/Vendor/Module/etc/events.xml <?xml version="1.0"?> <...
Prince Patel's user avatar
  • 22.5k
6 votes

How to check if customer is logged in or not?

I have get the best solution. It is based on authentication of customer. Some of the case customer session was not working, But every time my solution will work. Lets take a look. <?php $...
Rafiqul Islam's user avatar
6 votes
Accepted

Magento 2 - CurrentCustomer or Session?

Well, you can use either of them. Basically \Magento\Customer\Helper\Session\CurrentCustomer uses \Magento\Customer\Model\Session. The benefit of using the helper class is that it provides extra ...
Raphael at Digital Pianism's user avatar
6 votes

How to display customer name when logged in Magento 2?

Use the below function in your helper file with Magento\Customer\Model\Session to get session object in $customerSession public function getLoggedinCustomerName(){ if($customerSession->...
Vishwas Bhatnagar's user avatar
6 votes

Why Customer Session is not working on full page cache?

You can give a try to below code: 1). Using Object Manager $ObjectManager= \Magento\Framework\App\ObjectManager::getInstance(); $context = $ObjectManager->get('Magento\Framework\App\Http\Context'...
anonymous's user avatar
  • 3,724
5 votes

How to check if customer is logged in or not?

Hello got answer here : $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $customerSession = $objectManager->create('Magento\Customer\Model\Session'); if ($customerSession->...
Manish's user avatar
  • 3,096
5 votes
Accepted

What is the 'true' argument in session->getBeforeAuthUrl(true)?

$session->getBeforeAuthUrl(true) gives you the value for before_auth_url and deletes it from the session right after that. For reference see Mage_Core_Model_Session_Abstract_Varien::getData(). If ...
fmrng's user avatar
  • 3,388
5 votes

How to check if customer is logged in or not?

This is also one of the solution "Check whether the Customer is Logged in or not in Magento2" Try below code: $om = \Magento\Framework\App\ObjectManager::getInstance(); $context = $om->get('...
Soundararajan m's user avatar
5 votes

How to get CustomerId in API in magento 2? and how magento gets customerId for the same?

Please put below code of paramOverrider in your webapi definition where you need customer Id. <data> <parameter name="customer.id" force="true">%customer_id%</parameter> </...
Vipul Limbasiya's user avatar
5 votes
Accepted

Magento2 customer login and registration session not working in frontend in chome?

This is due to the cookie problem, not in browser but in Magento itself. In Magento, by default cookie’s lifetime is set to 3600 (1 hour). But if the end users computer time runs ahead of server’s ...
Rv Singh's user avatar
  • 1,617

Only top scored, non community-wiki answers of a minimum length are eligible