9

In an observer I'm setting a cookie like this:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cookieManager = $objectManager->get('Magento\Framework\Stdlib\CookieManagerInterface');

$cookieManager->setPublicCookie('myvar', $myVar);

Everything is working when Full page caching is disabled. However, once I enable it, the cookie won't be set.

How can I fix this?

2 Answers 2

2

OK. I asked the question on the official GitHub. It is quite normal if you are (like me) trying to access a cookie in a Block.

I found this topic and especially this one and hope this will help.

2

I managed to set the cookie the same way magento sets it for store by using plugins.

See file for plugin definition: vendor/magento/module-store/etc/di.xml:265

<type name="Magento\Framework\App\FrontController">
    <plugin name="storeCookieValidate" type="Magento\Store\Model\Plugin\StoreCookie" sortOrder="10"/>
</type>

And here you have the actual cookie set on beforeDispatch() vendor/magento/module-store/Model/Plugin/StoreCookie.php:69

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.