I'm trying to set a new cookie the same way the store cookie is set. I use the following plugin:

<type name="Magento\Framework\App\FrontController">
        <plugin name="homepage_plugin" type="My\Module\Model\Plugin\Hompage" sortOrder="100"/>

I have the following code that should set the cookie:

public function beforeDispatch(
        \Magento\Framework\App\FrontController $subject,
        \Magento\Framework\App\RequestInterface $request
    ) {
        $param = $request->getParam('___cookie');
        $cookie = $this->cookieManager->getCookie('cookie');
        $toSet = $param ? $param : $cookie;

        if (isset($param) && $param !== $cookie) {
            $metadata = $this->cookieMetadata

            $this->cookieManager->setPublicCookie('cookie', $toSet, $metadata);

Everything works fine on any other page except homepage. On homepage it seems that beforeDispatch() is called twice and the second time it's missing it's parameter on getParam().

With full_page disabled the cookie is set correctly on homepage as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.