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.


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)


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


$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


$block->getCatalogSession()->unsMyName();
$block->getCheckoutSession()->unsTestData();
$block->getCustomerSession()->unsTestHello();

From customer session, we can also fetch customer information like customer name and email.


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


// get checkout session data
echo $block->getCheckoutSession()->getQuoteId();
print_r($block->getCheckoutSession()->getQuote()->getData());

Using Object Manager


$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