{"id":1726,"date":"2021-04-24T05:27:15","date_gmt":"2021-04-24T05:27:15","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/get-set-and-unset-custom-session-variable-in-magento-2\/"},"modified":"2025-07-16T17:59:22","modified_gmt":"2025-07-16T12:29:22","slug":"get-set-and-unset-custom-session-variable-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/get-set-and-unset-custom-session-variable-in-magento-2\/","title":{"rendered":"How to Get, Set and Unset Custom Session Variable in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A session is a way of storing variables and making them available on multiple pages on a website. In other words, a session is a temporary object that is created on the server for each\u00a0Magento 2\u00a0store users to store some values, i.e., items in a cart.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The session enables you to build more customized applications and increase the appeal of your website visually, and it is a way to preserve certain data across subsequent accesses.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Sometimes while Magento 2 module customization or&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/create-module-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">module development of Magento 2<\/a>, you may need to&nbsp;get set and unset custom session variable in Magento 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For instance, once the user registers in our store, it stores the customer\u2019s first name, last name, id using session. Still, if you want to store additional information of the customer, i.e., age, then you have to use the method to get and set a custom session variable in&nbsp;Magento 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Suppose an owner wants a functionality where the popup should be displayed until the customer is logged in to the Magento 2 store. In that case, you have to unset the session variable when the customer logs out.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Check out the below method for the same:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Get Set and Unset Custom Session Variable in Magento 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Use the below code in your block 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\nnamespace Vendor\\Module\\Block\\Session;\nuse Magento\\Backend\\Block\\Template\\Context;\nuse Magento\\Checkout\\Model\\Session as checkoutSession;\nuse Magento\\Customer\\Model\\Session;\n\nprotected $checkoutSession;\nprotected $customerSession;\n\npublic function __construct(\n    Context $context,\n    Session $customerSession,\n    checkoutSession $checkoutSession,\n    array $data = []\n)\n{\n\n    $this->checkoutSession = $checkoutSession;\n    $this->customerSession = $customerSession;\n    parent::__construct($context, $data);\n}\n\npublic function getCheckoutSession()\n{\n    return $this->checkoutSession;\n}\n\npublic function getCustomerSession()\n{\n    return $this->customerSession;\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Now, we set and get session from template 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=\"\">$block->getCheckoutSession()->setCustomPrice('24.40');\necho $block->getCheckoutSession()->getCustomPrice(); \/\/ To get custom price value\n\n$block->getCustomerSession()->setCustomName('test');\necho $block->getCustomerSession()->getCustomName(); \/\/ To get custom name value<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. To Unset session variable<\/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=\"\">$block->getCheckoutSession()->unsCustomPrice();\n$block->getCustomerSession()->unsCustomName();<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can also find all details from customer session, checkout session, quote related details and sessions of catalog , backend, newsletter.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, please 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>A session is a way of storing variables and making them available on multiple pages on a website. In other words, a session is 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-1726","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1726","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=1726"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1726\/revisions"}],"predecessor-version":[{"id":18102,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1726\/revisions\/18102"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1726"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}