How to set, unset session in Magento 2

In this topic, I will show you how to set, unset, session in Magento 2 which will help you to build as well as eradicate the session by the rapid way through executing the code. Because with the custom Magento 2 development, it is very necessary to learn about the session

The following are the list of session types in Magento 2

vendor/magento/module-checkout/Model/Session.php

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

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

{ 
 $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 feasible to locate them from .phtml file.

$block->getCatalogSession()->setMyName('Webnexs');
echo $block->getCatalogSession()->getMyName() . '<br />'; // output: Webnexs


$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:

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

Particularly, Customer session permits gathering 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());

The following steps help you to set, unset session in Magento 2.

Last Update: April 6, 2018  

February 20, 2018   509   Nandini R    Magento2    
Total 1 Votes:
0

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Google+
http://www.webnexs.com/blog/kb/set-unset-session-magento-2
Twitter
LinkedIn