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 ...
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\...
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 ...
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
){
$...
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 ...
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 ...
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;
...
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\...
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 ...
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()-&...
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(
....
...
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 ...
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 ...
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\...
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\...
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 ...
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;
...
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 ...
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 ...
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(\...
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"?>
<...
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
$...
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 ...
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->...
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'...
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->...
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 ...
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('...
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>
</...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
customer-session × 239magento2 × 108
magento-1.9 × 51
session × 47
customer × 41
customer-account × 34
login × 30
magento-2.1 × 12
full-page-cache × 12
magento2.3 × 11
cookie × 11
logout × 9
checkout × 8
event-observer × 8
customer-login × 8
php × 7
cart × 7
cache × 7
checkoutsession × 6
magento-enterprise × 5
multistore × 5
redirect × 5
sessions × 5
magento2.2 × 4
blocks × 4