{"id":1732,"date":"2021-04-27T10:49:47","date_gmt":"2021-04-27T10:49:47","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/get-customer-id-without-using-session-in-magento-2\/"},"modified":"2025-05-22T10:36:12","modified_gmt":"2025-05-22T05:06:12","slug":"get-customer-id-without-using-session-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/get-customer-id-without-using-session-in-magento-2\/","title":{"rendered":"How to Get Customer ID Without Using Session in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento developers often use customer ID to implement features that are exclusively for logged in customers. Even I have posted solutions where one can&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/get-customer-addresses-by-customer-id-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">get customer addresses by customer ID in Magento 2<\/a>&nbsp;and use it to maintain the address database while offering guest checkout. Magento 2 provides a convenient way to&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/get-set-and-unset-custom-session-variable-in-magento-2\/\">get, set, and unset custom session variables<\/a>&nbsp;using the session object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can&nbsp;<em><strong>get customer ID without using session in Magento 2<\/strong><\/em>&nbsp;and hence avoid issues like<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A NULL value is returned even if the customer is logged in.<\/li>\n\n\n\n<li>You have to disable the cache, and hence low page speed.<\/li>\n\n\n\n<li>It does not return customer ID.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s why I recommend using the below solution&nbsp;which does not generate any issues and doesn\u2019t require disabling cache.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Get Customer ID Without Using Session in Magento 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Use the below code in the helper file.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendor\\Extension\\Helper;\n\nuse Magento\\Authorization\\Model\\UserContextInterface;\nuse Magento\\Framework\\App\\Helper\\AbstractHelper;\nuse Magento\\Framework\\App\\Helper\\Context;\n\nclass Data extends AbstractHelper\n{\n    protected $userContext;\n\n    public function __construct(\n        Context $context,\n        UserContextInterface $userContext\n    )\n    {\n        $this->userContext = $userContext;\n        parent::__construct($context);\n    }\n\n    public function getLoginCustomerId()\n    {\n        $customerId = $this->userContext->getUserId();\n        return $customerId;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s all!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feel free to share the solution with Magento Community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank You.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento developers often use customer ID to implement features that are exclusively for logged in customers. Even I have posted solutions where one can&nbsp;get customer&#8230;<\/p>\n","protected":false},"author":14,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1732","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1732","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1732"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1732\/revisions"}],"predecessor-version":[{"id":14358,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1732\/revisions\/14358"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1732"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1732"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1732"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}