1

Following program is used to set value of custom session variable country.

<?php

namespace Vendor\Module\Controller\Index;


use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ResponseInterface;

class Country extends \Magento\Framework\App\Action\Action
{
    public function __construct(
        Context $context,
        \Magento\Framework\Session\SessionManagerInterface $sessionManager
        )
    {
        $this->session = $sessionManager;
        parent::__construct($context);
    }

    public function execute()
    {
        $countryId = $this->getRequest()->getParam('country');
        $this->session->start();
        $this->session->setCountry($countryId);
    }
}

Now, how I can get country's value in any javascript file?

  • 1
    You have to set that session value to a Cookie variable then access that using javascript get that value. – Amit Bera Feb 20 '19 at 8:27
0

As Amit Bera suggested in the comment section, I have added country's value to cookie and I was able to get it on other pages as given below:

Controller class to set cookie value:

namespace Vendor\Module\Controller\Index;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Session\SessionManagerInterface;
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
use Magento\Framework\Stdlib\CookieManagerInterface;

class Country extends \Magento\Framework\App\Action\Action
{
    const COOKIE_NAME = 'country';

    /**
     * Country constructor.
     * @param Context $context
     * @param CookieManagerInterface $cookieManager
     * @param CookieMetadataFactory $cookieMetadataFactory
     */
    public function __construct(
        Context $context,
        CookieManagerInterface $cookieManager,
        CookieMetadataFactory $cookieMetadataFactory,
        SessionManagerInterface $sessionManager
        )
    {
        $this->cookieManager = $cookieManager;
        $this->cookieMetadataFactory = $cookieMetadataFactory;
        $this->sessionManager = $sessionManager;
        parent::__construct($context);
    }

    public function execute()
    {
        $countryId = $this->getRequest()->getParam('country');
        $this->set($countryId);
        return;
    }

    /**
     * @param string $value
     * @param int $duration
     * @return void
     */
    public function set($value, $duration = 86400)
    {
        $metadata = $this->cookieMetadataFactory
            ->createPublicCookieMetadata()
            //->setDuration($duration)
            ->setPath($this->sessionManager->getCookiePath())
            ->setDomain($this->sessionManager->getCookieDomain());

        $this->cookieManager->setPublicCookie(
            self::COOKIE_NAME,
            $value,
            $metadata
        );
    }
}

Get cookie value in js file as given below:

require(['jquery'], function ($) {    
var country = $.cookie('country');
console.log(country);
});

You can also open your browser's console on other pages of the site and run console.log(document.cookie); to check it's value.

If you do not need to call your controller class, you can set cookie value in your js file itself as given below:

function setCountry(country_value) {
    require(['jquery'], function ($) {    
    $.cookie('country', country_value, '/'); // set cookie value
    var country = $.cookie('country'); // get cookie value
    console.log(country);
    });
}
| improve this answer | |

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.