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?

1 Answer 1

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.

1
  • Thanks for your answer but as you said it will impact the performance :| Commented Apr 10, 2018 at 4:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.