I already referred

How to Set, Retrieve and Unset Session Variables in Magento 2?

When a user clicks on "Add To Cart" button. I would like to Add One Additional Variable in Cart Quote Session.

How to set it & where can I do this may be Observer not sure?

  • 1
    you have to do observer for it and inside observer you have to call it. – Rakesh Jesadiya Sep 29 '16 at 4:43

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.

echo $block->getCheckoutSession()->getTestData() . '<br />'; // output: 123

Unset session


From checkout session, we can fetch quote information.

// get checkout session data
echo $block->getCheckoutSession()->getQuoteId();
  • Would like to set Extra Variable with Each Product. Suppose there are 3 products in Cart. Then it's gonna set with Each Product – Jackson Sep 29 '16 at 6:45
  • you will get same variable whatever you are setting in set data you will get only data . its static data only Just get & Set . if you want dynamic then you have to create product attribute & get those data dynamic – Brad Hodge Sep 29 '16 at 6:47
  • It's work like a charm. Thanks @Ankit Jaiswal – Jackson Oct 4 '16 at 1:34
  • Great Ankit Shah :) – Brad Hodge Oct 4 '16 at 4:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.