Use the Below Code in your Custom Module to Set, Unset Session
protected $_checkoutSession;
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Checkout\Model\Session $checkoutSession,
array $data = []
)
{
$this->_checkoutSession = $checkoutSession;
parent::__construct($context, $data);
}
public function getCheckoutSession()
{
return $this->_checkoutSession;
}
Now, we set and get session from template (.phtml) file.
$block->getCheckoutSession()->setTestData('123');
echo $block->getCheckoutSession()->getTestData() . '<br />'; // output: 123
Unset session
$block->getCheckoutSession()->unsTestData();
From checkout session, we can fetch quote information.
// get checkout session data
echo $block->getCheckoutSession()->getQuoteId();
print_r($block->getCheckoutSession()->getQuote()->getData());