Search code examples
magentomagento2

Magento 2 session unsetting itself


I need to be able to store some custom session variables that exist for the custom, regardless of whether they're signed in or not, but for some reason my sessions keep deleting themselves.

I have used this example to help me add my session code in.

Here's my code

Block file

<?php

namespace MyVendor\MyModel\Block;

use Magento\Framework\View\Element\Template;

class ProductSearch extends Template {

    protected $_customSession;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Customer\Model\Session $customSession,
        array $data = []
    ){
        parent::__construct($context, $data);
        $this->_customSession = $customSession;
    }

    //Get the car model from the session
    public function getSessionCarModel(){
        return $this->_customSession->getCarModel();
    }

    //Unset the car model from the session
    public function unsetSessionCarModel(){
        return $this->_customSession->unsCarModel();
    }

}

and heres the top of my template file that sorts the session when its loaded

productsearchbanner.phtml

<?php

    //If the user has selected a new model, unset our session then start a new one
    if(isset($_POST['modelSelect'])){
        //Unset the other sessions
        $block->unsetSessionCarModel();
        //Set the model session
        $block->setSessionCarModel($_POST['model']);
    }
    echo '<pre>';
    var_dump($_SESSION);
    echo '</pre>';
?>

The way the code is meant to work is, if the $_POST['modelSelect'] is set, the user has come from the model select page so we need to start the process again and reset their session, but if they haven't, the session should remain the same.

My issue is, when I come from the model select page, my session variable shows in the var dump no problem, as shown below.

enter image description here

But then as soon as I go to any another page on my site (for example, the homepage) and then back to the product search page, the session has cleared?

enter image description here

What am I doing wrong? Why does my session clear every time I load a page? I just need to be able to set the equivalent of $_SESSION['carModel'] and it be persistent for that user, regardless of if they're logged in or not, or where on the site they go.

Can someone please point me in the right direction?


Solution

  • Setting sessions in Blocks or Template files is a problem. This is because of full page cache. Magento's execution cycle changes with FPC turned on.

    Controllers or Models are the best places to update session data.

    But, if you need to update your session in a template / block, then you can call a custom action via AJAX and have it update the session info. Generally, one would need to take these steps in Magento 2:

    • create a new controller / action pair in an existing or a new module, that would update session info. This controller should ideally accept AJAX requests only.

    • have a template rendered in container before_body_end and toss some jQuery code in there, that will query the controller / action pair to have the session info updated.

    This way, whenever the page will load, it will trigger a session update ( or you can have it trigger on any other event, say when a user clicks something etc. ) by requesting your controller / action, say, /my-module/my-controller/my-session-updater-action.