{"id":573,"date":"2019-08-28T19:44:08","date_gmt":"2019-08-28T19:44:08","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/08\/28\/check-if-a-customer-is-logged-into-magento-2\/"},"modified":"2025-05-22T16:04:47","modified_gmt":"2025-05-22T10:34:47","slug":"check-if-a-customer-is-logged-into-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/check-if-a-customer-is-logged-into-magento-2\/","title":{"rendered":"How to Check if a Customer is Logged into Magento  2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The post gives the solution to get billing &amp; shipping details programmatically from quote in Magento 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The developers can use this solution in a number of ways.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I\u2019ll share how I put it to use.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One of the clients was troubled with the high rate of shopping cart abandonment. His marketing team asked to use the abandon cart details and send a series of quote emails to those potential customers who quit his store, convincing him for purchase.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For sending such Emails, he required to get the shipping and billing details that were to be included in the Email so they can directly check out the order without the hassle of adding the required shipping details!! (You know I always talk about crazy client requirements, this being one of them  )<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So, I had to get that data programmatically as to not waste time and it being the only feasible way to do, I came up with the below solution.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Get Billing &amp; Shipping Details Programmatically From Quote in Magento 2:<\/h2>\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\nnamespace Vendor\\Extension\\Controller\\Customer;\nuse Magento\\Framework\\App\\Action\\Action;\nuse Magento\\Framework\\App\\Action\\Context;\nuse Magento\\Customer\\Model\\Session;\nuse Magento\\Framework\\App\\Http\\Context as AuthContext;\nclass Index extends Action\n{\n    private $customerSession;\n    private $authContext;\n    public function __construct(Context $context, Session $session, AuthContext $authContext)\n    {\n        $this->customerSession = $session;\n        $this->authContext = $authContext;\n        parent::__construct($context);\n    }\n    public function execute()\n    {\n        \/\/ by using Session model\n        if ($this->customerSession->isLoggedIn()) \n        {\n            \/\/ customer login code\n        }\n        else\n        {\n            \/\/ customer not login\n        }\n        \/\/ using HTTP context\n        $isLoggedIn = $this->authContext->getValue(\\Magento\\Customer\\Model\\Context::CONTEXT_AUTH);\n        if ($isLoggedIn) \n        {\n            \/\/your coding\n        }\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:&nbsp;<\/strong>When PageCache module is on (probably always on production), keep in mind that as soon as layout generation starts, customer session will be cleared by <strong>MagentoPageCacheModelLayoutDepersonalizePlugin::afterGenerateXml<\/strong> on all cacheable pages. It means that if you now check the login status of the customer in via HTTP context, it will still say \u2018yes\u2019, but customer data will not be available in customer session anymore. Make it a point to double-check before trying to access data in customer session. This can easily happen in the block, while is unlikely in action controller since you are not expected to generate layout manually there. It will be generated after the action controller returns an instance of ResultInterface.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">For Magento 2.3.x, use the below JS method to check if the customer is logged into Magento 2:<\/h3>\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=\"\">define([\n    'uiComponent',\n    'Magento_Customer\/js\/model\/customer'\n], function (\n    Component,\n    customer\n) {\n    'use strict';\n \n    return Component.extend({\n      \n        \/**\n         * Check if customer is logged in\n         *\n         * @return {boolean}\n         *\/\n        isLoggedIn: function () {\n            return customer.isLoggedIn();\n        }\n    });\n});<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Use any of the above methods to check if the user is logged in or not in Magento 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Similarly, you can check if a <a href=\"https:\/\/meetanshi.com\/blog\/programmatically-check-if-customer-account-is-confirmed-or-not-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">customer account is confirmed or not in Magento 2<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feel free to share the post with fellow developers on social media.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The post gives the solution to get billing &amp; shipping details programmatically from quote in Magento 2. The developers can use this solution in a&#8230;<\/p>\n","protected":false},"author":5,"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-573","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/573","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\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=573"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/573\/revisions"}],"predecessor-version":[{"id":15289,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/573\/revisions\/15289"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}