cancel
Showing results for 
Search instead for 
Did you mean: 

session not getting value from phtml to block class magento 2

   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

session not getting value from phtml to block class magento 2

Please someone review my code and help me with suitable code or answer.
I need serious help from community.

Here's the link of code:
https://magento.stackexchange.com/questions/245457/session-not-getting-value-from-phtml-to-block-cla...

 

4 REPLIES 4

Re: session not getting value from phtml to block class magento 2

Hello @jhon_doe2,

 

 

I found the equivalent way for this in Magento2:

Mage::getSingleton('core/session')->setMyValue('test');
Mage::getSingleton('core/session')->unsMyValue();

Set/Get/Unset Value in Core Session:

protected $_coreSession;

public function __construct(
    -----
    \Magento\Framework\Session\SessionManagerInterface $coreSession
    ){
    $this->_coreSession = $coreSession;
    ----
}

public function setValue(){
    $this->_coreSession->start();
    $this->_coreSession->setMessage('The Core session');
}

public function getValue(){
    $this->_coreSession->start();
    return $this->_coreSession->getMessage();
}

public function unSetValue(){
    $this->_coreSession->start();
    return $this->_coreSession->unsMessage();
}


By this way we can set custom values if our session value is not related to below sessions:

  • \Magento\Backend\Model\Session
  • \Magento\Catalog\Model\Session
  • \Magento\Checkout\Model\Session
  • \Magento\Customer\Model\Session
  • \Magento\Newsletter\Model\Session

--
If my answer is useful, please Accept as Solution & give Kudos

Re: session not getting value from phtml to block class magento 2

Mage::getSingleton

This function is not working in my case.

Re: session not getting value from phtml to block class magento 2

Kindly use \Magento\Framework\Session\Generic class for default session,

    public function __construct(
            \Magento\Quote\Model\QuoteValidator $quoteValidator,
            \Magento\Framework\Session\Generic $session
        )  {
            $this->quoteValidator = $quoteValidator;
            $this->_coreSession = $coreSession;
        }
    
        public function getValue()
        {
            return $this->_coreSession->getMyValue(); //Get value from customer session
        }
    
        public function setValue($total_cost)
        {
            return $this->_coreSession->setMyValue($total_cost);
        }

 

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

Re: session not getting value from phtml to block class magento 2

Hello @jhon_doe2,

 

Please use getModel function on it.

 

--
If my answer is useful, please Accept as Solution & give Kudos