Magento 2: Set Unset Get Session
This article shows how to create and destroy different types (Checkout, Customer, Catalog) of sessions in Magento 2.
As you can see below, there are different types of session classes present in Magento 2.
1 2 3 4 5 6 7 8 9 10 11 12 13 | vendor/magento/module-catalog/Model/Session.php vendor/magento/module-newsletter/Model/Session.php vendor/magento/module-persistent/Model/Session.php vendor/magento/framework/Message/Session.php vendor/magento/module-customer/Model/Session.php vendor/magento/module-backend/Model/Session.php vendor/magento/module-checkout/Model/Session.php |
In below example code, I will be dealing with Catalog, Customer, and Checkout sessions.
Both Dependency Injection and Object Manager way of coding are shown below.
Using Dependency Injection (DI)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <?php namespace Chapagain\HelloWorld\Block; class HelloWorld extends \Magento\Framework\View\Element\Template { protected $_catalogSession; protected $_customerSession; protected $_checkoutSession; public function __construct( \Magento\Backend\Block\Template\Context $context, \Magento\Catalog\Model\Session $catalogSession, \Magento\Customer\Model\Session $customerSession, \Magento\Checkout\Model\Session $checkoutSession, array $data = [] ) { $this->_catalogSession = $catalogSession; $this->_checkoutSession = $checkoutSession; $this->_customerSession = $customerSession; parent::__construct($context, $data); } public function _prepareLayout() { return parent::_prepareLayout(); } public function getCatalogSession() { return $this->_catalogSession; } public function getCustomerSession() { return $this->_customerSession; } public function getCheckoutSession() { return $this->_checkoutSession; } } ?> |
Now, we set and get session from template (.phtml) file.
1 2 3 4 5 6 7 8 | $block->getCatalogSession()->setMyName('Mukesh Chapagain'); echo $block->getCatalogSession()->getMyName() . '<br />'; // output: Mukesh Chapagain $block->getCheckoutSession()->setTestData('123'); echo $block->getCheckoutSession()->getTestData() . '<br />'; // output: 123 $block->getCustomerSession()->setTestHello('456'); echo $block->getCustomerSession()->getTestHello() . '<br />'; // output: 456 |
Unset session
1 2 3 | $block->getCatalogSession()->unsMyName(); $block->getCheckoutSession()->unsTestData(); $block->getCustomerSession()->unsTestHello(); |
From customer session, we can also fetch customer information like customer name and email.
1 2 3 4 5 6 7 8 9 | // get customer data if ($block->getCustomerSession()->isLoggedIn()) { $customerId = $block->getCustomerSession()->getCustomerId(); $customerData = $block->getCustomerSession()->getCustomer(); echo $customerId . '<br />'; echo $customerData->getFirstname() . ' ' . $customerData->getLastname() . '<br />'; echo $customerData->getEmail() . '<br />'; print_r($block->getCustomerSession()->getCustomer()->getData()); } |
From checkout session, we can fetch quote information.
1 2 3 | // get checkout session data echo $block->getCheckoutSession()->getQuoteId(); print_r($block->getCheckoutSession()->getQuote()->getData()); |
Using Object Manager
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $catalogSession = $objectManager->get('\Magento\Catalog\Model\Session'); $customerSession = $objectManager->get('\Magento\Customer\Model\Session'); $checkoutSession = $objectManager->get('\Magento\Checkout\Model\Session'); // set session variables and their value $catalogSession->setMyName('Mukesh Chapagain'); $checkoutSession->setTestData('123'); $customerSession->setTestHello('456'); // print session variables value echo $catalogSession->getMyName() . '<br />'; // output: Mukesh Chapagain echo $checkoutSession->getTestData() . '<br />'; // output: 123 echo $customerSession->getTestHello() . '<br />'; // output: 456 // Unset session $catalogSession->unsMyName(); $checkoutSession->unsTestData(); $customerSession->unsTestHello(); |
Hope this helps. Thanks





Mukesh Chapagain is a graduate of Kathmandu University (Dhulikhel, Nepal) from where he holds a Masters degree in Computer Engineering. Mukesh is a passionate web developer who has keen interest in open source technologies, programming & blogging.