Magento 2: How to Check if a Customer is Logged in?
As the title indicates, this article discusses everything there is to know about ‘Magento 2 check if customer is logged in’.
The first question on your mind after reading the title is why would a store owners want to check if customer is logged in Magento 2. Well, competition in eCommerce is at an all time high and in the coming years, this competition will only increase further. What does it mean? As a store owner, you need to find a way to retain and attract customers.
This is only possible through the right strategies. After all, product quality is no longer the differentiating factor. Almost every online store offers more or less the same quality products. Our experience as a Magento extension company tells us that customer personalisation is the key to long-term success.
When we say personalisation, the first thing that comes into mind is artificial intelligence. No doubt AI can boost your eCommerce sales. Check out the below infographic on how AI boosts eCommerce.
Instead of focusing your resources on AI, we have a much simpler solution. All you need to do is start treating your customers differently. For starters, treat logged in and non-logged in customers differently. For example, you may want to offer exclusive features or discounts to logged in customers only.
To implement this, you first need to confirm whether a Magento 2 customer is logged in or not? Before we talk about ‘Magento 2 check if customer is logged in’, let’s talk about the Magento 2 login as customer extension. Afterwards, we’ll discuss how to check if customer is logged in Magento 2.
About this Extension
Why are we mentioning this extension? Too often, we hear store owners complain about poor reviews, lack of sales, and low engagement. They argue that their store has the best features, top quality products, and robust marketing strategies. Despite it, the store’s performance is lower than expectations and peers.
Unfortunately, they fail to understand the most important aspect of any business – the customer. Unless a business understands the customer, they cannot succeed. Therefore, the emphasis must be on understanding the customer’s pain points, buying journey, and other aspects.
With this extension, you can login to your store as a customer and experience their journey firsthand. There is no need to obtain customer credentials.
This allows you to make changes to their account or help them with any issue. For example, a user may be experiencing a problem but unable to report. Your Magento experts can log in to their account, identify the problem, and fix it promptly. All this without the customer having to worry about sharing their credentials.
Book a Live Demo here.
Other Useful Extensions for Personalisation:
Methods To Check If a Customer Is Logged-In in Magento 2
There are 2 ways you can check this. By injecting class and by using object manager. Although the 2nd method (object manager) is not recommended, it’s still used as an alternative.
Method 1: By Injecting Class (Dependency Injection)
In this method, first, you need to inject the following class in the constructor method:
/Magento/Customer/Model/Session
protected $_session;
public function __construct(
...
\Magento\Customer\Model\Session $session,
...
) {
...
$this->_session = $session;
...
}
Then in your class, use the following code in any function to check if the customer is logged in or not:
if ($this->_session->isLoggedIn())
{
// Customer is logged in
} else
{
// Customer is not logged in
}
Sample Code:
?php
namespace [Vendor]\[Module]\Controller\Index;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Action\Action;
use Magento\Customer\Model\Session;
class ClassName extends Action
{
protected $_session;
public function __construct(Context $context, Session $session)
{
parent::__construct($context);
$this->_session = $session;
}
public function execute()
{
// by using Session model
if($this->_session->isLoggedIn()) {
//customer has logged in
// your code in here
}else{
//Customer is not logged in
// your code in here
}
}
}
Method 2: Using Object Manager
You can use this method as an alternative to the above.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn())
{
// customer login action
}
Final Thoughts on Magento 2 Check if Customer is Logged In
You can always engage our expert Magento developers if you encounter any issues with the above code. Feel free to contact us in case you have any issue in your Magento 2 store.
FAQs About Magento 2 Check if Customer is Logged In
What is the importance of Magento 2 check if customer is logged in?
The Magento 2 check if customer is logged in feature allows merchants to personalise the customer shopping experience. For example, store owners can offer different prices for not logged in and logged in customers. Likewise, they can offer them special discounts to encourage user registration.
How can Magento 2 check if customer is logged in?
There are two ways to do it – the Dependency Injection and the Object Manager. The latter method is not recommended since it goes against Magento 2 best coding practices.
Is there a built-in feature for Magento 2 check if customer is logged in?
No, there is no built-in feature for this purpose. However, developers can use session-based methods to check the status.
What do I do if Magento 2 guest checkout not working?
If you are having trouble with Magento 2 guest checkout not working, then first clear cache and then reindex. If the error appeared after installing an extension, then we recommend disabling the extension and reaching out to the developer for a fix. Also, check if Guest Checkout is set to ‘Yes’. You can do so by Stores > Configuration > Sales > Checkout. Under Guest Checkout, ensure Allow Guest Checkout is set to Yes.
Can I hide prices for logged in and guest users?
Yes, you can do so by using third-party extensions such as Magento 2 hide prices for guest users by FME.
Read More Magento 2 Blogs:
This blog was created with FME's SEO-friendly blog