Skip to main content
OverflowAI is here! AI power for your Stack Overflow for Teams knowledge community. Learn more
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
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

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
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
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
14 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.4k
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
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,114
11 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
  • 290
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,116
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,617
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,235
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,246
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.7k
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,752
6 votes
Accepted

How to show Block depending on Customer Logged In/Out in Magento 2

I don't know is it a right way, but you can write a simple module which will add similar handles: app/code/MageWorx/CustomerLayoutHandle/etc/frontend/di.xml <config xmlns:xsi="http://www.w3....
Siarhey Uchukhlebau's user avatar
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,116
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
Accepted

Difference between Session and SessionFactory in Magento 2

There are two ways of creating object: 1) Model \Magento\Customer\Model\Session 2) Factory \Magento\Customer\Model\SessionFactory If you use Model class, it will always create shared objects it ...
Prince Patel's user avatar
  • 22.7k
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
4 votes
Accepted

Suddenly started using another logged in user's session - Magento 2

I was able to replicate this issue by clicking a link with a "SID" (session ID) in it. If that session ID belonged to another customer and they were logged in, I could see their details. Because the ...
Wireblue's user avatar
  • 164
4 votes
Accepted

load customer object update issue in magento 2?

Use factory pattern that is bridge to connect incomplete object in magento 2. use below code. I have changed customer model to factory pattern class. That is auto generated code concept of magento 2 ...
Prashant Valanda's user avatar

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