Shopping cart

How to Set, Unset Session in Magento 2

Topic How to set and unset session in Magento 2 will help you know how to create as well as remove the session by the quick way via running the code. Because with the custom Magento 2 Development, it is very necessary to learn about the sessions.

First of all, please take a look at a list of session types in Magento 2


Next, you will start calling Catalog, Customer, and Checkout sessions through the following code.

namespace Mageplaza\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;

Then, with Catalog, Customer, and Checkout sessions, it is possible to set them from .phtml file.

echo $block->getCatalogSession()->getMyName() . '<br />'; // output: Mageplaza
$block->getCheckoutSession()->setTestData('Hello World');
echo $block->getCheckoutSession()->getTestData() . '<br />'; // output: Hello World
$block->getCheckoutSession()->setTestHello('Test Hello Value');
echo $block->getCheckoutSession()->getTestHello() . '<br />'; // output: Test Hello Value

In case you want to unset those sessions, let do as the following:


Specifically, Customer session allows collecting the 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 />';

Checkout session will show the quote information.

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

This is the guide to set and unset session in Magento 2. If that you have any queries about the article or any questions in general, use the comment section below!

Related Topics

Enjoyed the tutorial? Spread it to your friends!


Sam Thomas
Sam Thomas
CEO and Founder of Mageplaza. Pursueing a simple and healthy lifestyle. A friend, a husband and a dad of two children, a trainer and a wanna-be influencer. He is a big fan of sports and travel, also.

Comments for How to Set, Unset Session in Magento 2

Please leave comments if you have any questions, feedbacks.

Module Development

We recommend:

People also searched for

  • magento 2 set unset session
  • magento 2 how to set unset session
  • set unset session magento 2
  • set unset session in magento 2
  • 2.1.x, 2.2.x, 2.3.x