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?

Khoa Truong
32.5k11 gold badges91 silver badges159 bronze badges
asked Apr 9, 2018 at 13:37

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.

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.