How to Set, Unset Session in Magento 2

Today in the topic How to set and unset session in Magento 2 we’ll talk about how to set and unset the session ina quick way via running the code. With the custom Magento 2 Development and customization, it is sometimes very necessary to get the session information. Learning about the sessions to adjust them anytime to acquire important information within a certain amount of time is necessary for Magento 2 store admins.

Magento 2 session types

So what is a session in Magento 2?

A session is a way of sorting and storing variables on every page of Magento 2 stores. It allows your users to temporarily store some values on your store such as items in the shopping carts, personal information, etc.

When a user registers an account in your store, the session allows you to store their information like first name, last name, or ID. Then, by setting a custom session variable in Magento 2, you can store more additional information.

Now, let’s take a look at a list of session types 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

Check out the below method to set or unset a session in Magento 2 anytime you want easily and quickly.

Set sessions in Magento 2

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

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

$block->getCatalogSession()->setMyName('Mageplaza');
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

Unset sessions in Magento 2

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

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

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 />';
    print_r($block->getCustomerSession()->getCustomer()->getData());
}

Checkout session will show the quote information.

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

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!

Conclusion

This is a short and useful guide to set and unset sessions in Magento 2. If you have any queries about the article or any questions in general, feel free to leave the comment below! We’ll get back to you soon.

We hope this is useful for you and the above code is working fine on your Magento 2 store admin. Many practical tutorials are coming your way. Thanks for reading.

Related Topics

Image Description
With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.

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.3.x, 2.4.x
x