Skip to content

Cant set data to Session

i have trouble when i’m trying to save data to session,i send ajax request to my custom controller,save variable to session object, but when i’m getting data on my custob block, it returns nothing

/**
 * @var StoreManagerInterface
 */
protected  StoreManagerInterface $storeManager;

protected Session $session;

/**
 * @var SessionManagerInterface
 */
protected SessionManagerInterface $sessionManager;

/**
 * @var RequestInterface
 */
protected RequestInterface $request;

/**
 * @var ResultFactory
 */
protected ResultFactory $resultFactory;

/**
 * @param SessionManagerInterface $sessionManager
 * @param RequestInterface $request
 * @param StoreManagerInterface $storeManager
 * @param ResultFactory $resultFactory
 */
public function __construct(
    Session $session,
    SessionManagerInterface $sessionManager,
    RequestInterface $request,
    StoreManagerInterface $storeManager,
    ResultFactory $resultFactory
){
    $this->session = $session;
    $this->sessionManager = $sessionManager;
    $this->request = $request;
    $this->storeManager = $storeManager;
    $this->resultFactory = $resultFactory;
}


public function execute()
{
    $response = $this->resultFactory->create(ResultFactory::TYPE_JSON);
    $currencyCode=$this->request->getParam('currencyCode');
    try {
        $this->storeManager->getStore()->setCurrentCurrencyCode($this->request->getParam('currencyCode'));
        $this->session->setData('currencyCode',$currencyCode);

        $response->setData(['status' => true]);
    }
    catch (NoSuchEntityException $e) {
        $response->setData(['status' => false]);
    }
    return $response;
}

}