That’s why I recommend using the below solution which does not generate any issues and doesn’t require disabling cache.
If you have any doubts regarding this post, just mention them in the Comments section below.
I can go one with the examples where a Magento developer will require to get a customer ID. But today, instead I am going to offer an alternate solution to get a customer ID.
- A NULL value is returned even if the customer is logged in.
- You have to disable the cache, and hence low page speed.
- It does not return customer ID.
Something went wrong.
Method to Get Customer ID Without Using Session in Magento 2
Magento developers often use customer ID to implement features that are exclusively for logged in customers. Even I have posted solutions where one can get customer addresses by customer ID in Magento 2 and use it to maintain the address database while offering guest checkout.
class Data extends AbstractHelper
public function __construct(
$this->userContext = $userContext;
public function getLoginCustomerId()
$customerId = $this->userContext->getUserId();
You can get customer ID without using session in Magento 2 and hence avoid issues like
Feel free to share the solution with Magento Community via social media.
Thank you for subscribing.
Apart from this, the admin may need a customer ID to hide the signup button for already logged in customers, or display a discount offer that is valid only for logged in customers!
Get Weekly Updates
I would be happy to help.
Never miss Magento tips, tricks, tutorials, and news.
Use the below code in the helper file.