How to Get Customer ID Without Using Session in Magento 2

How 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.

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!

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.

You can get customer ID without using session in Magento 2 and hence avoid issues like

  • 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.

That’s why I recommend using the below solution which does not generate any issues and doesn’t require disabling cache.

Method to Get Customer ID Without Using Session in Magento 2

Use the below code in the helper file.

That’s all!

If you have any doubts regarding this post, just mention them in the Comments section below.

I would be happy to help.

Feel free to share the solution with Magento Community via social media.

Thank You.

Get Weekly Updates

Never miss Magento tips, tricks, tutorials, and news.

Thank you for subscribing.

Something went wrong.

5
(based on 7 Reviews)

He has been with Meetanshi for more than two years now as a Magento developer. A silent guy whom you can always find solving clients' issues, is an avid reader too.

Leave a reply:

Your email address will not be published.