I've got a couple controller that I'm trying to use to save and retrieve a custom piece of session data. It works on my local development environment but doesn't work on my staging environment. Here's the specs for my local environment:
- Apache 2.4
- PHP 7.0.16
- Magento 2.1.5
- Mysql 5.7
Here's the spec's for my staging environment
- Apache 2.2
- PHP 7.0.15
- Mysql 5.6
- Magento 2.1.5
- Redis
- Varnish
Here's my get controller:
<?php
namespace Foo\StoreLocator\Controller\Store;
class Get extends \Magento\Framework\App\Action\Action
{
/**
* @var \Magento\Customer\Model\Session
*/
protected $customerSession;
/**
* Save constructor.
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Customer\Model\Session $customerSession
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Customer\Model\Session $customerSession
){
$this->customerSession = $customerSession;
parent::__construct($context);
}
public function execute()
{
$selectedStore = $this->customerSession->getSelectedStore();
return $this->resultFactory
->create(\Magento\Framework\Controller\ResultFactory::TYPE_JSON)
->setData($selectedStore);
}
}
Here's my set controller:
<?php
namespace Foo\StoreLocator\Controller\Store;
class Save extends \Magento\Framework\App\Action\Action
{
/**
* @var \Magento\Customer\Model\Session
*/
protected $session;
/**
* Save constructor.
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Customer\Model\Session $customerSession
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Customer\Model\Session $customerSession
){
$this->session = $customerSession;
parent::__construct($context);
}
/**
* @return void
*/
public function execute()
{
$data = $this->getRequest()->getPostValue();
$this->session->setSelectedStore($data);
return;
}
}
I'm calling the controllers via AJAX call on product details pages. On my staging environment, the getter always returns null. I thought maybe it was an issue with full page cache being enabled on my staging environment because I don't use any caching on my local environment, but I disabled full page cache on staging and the issue persists.
Any ideas what's going on and/or how to fix this issue? I've reworked things to use a cookie but I'd really like to use the customer session.