0

Is there any way to access custom data stored in a customer session from a JS file ( without Ajax call ).

Here is the code for controller:

<?php

namespace Company\Vendor\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Customer\Model\Session;

class Index extends Action
{
    protected $_customerSession;

    public function __construct(
        Context $context,
        Session $customerSession
    ) {
        parent::__construct($context);
        $this->_customerSession = $customerSession;
    }

    public function execute()
    {
        $this->_customerSession->setMyValue('yes');
    }
}

To access the MyValue value, we can use $this->_customerSession->getMyValue();

How can I get the MyValue value in a JS file?

0

[Not sure it's the correct way or best way] We need to set uncacheable page (will impact the performance). This is because when enabling the full page cache, the customer session will be clear.

  • Thanks for your answer but as you said it will impact the performance :| – vedu Apr 10 '18 at 4:13

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.