1

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"/>
</type>

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
                ->createPublicCookieMetadata()
                ->setDurationOneYear()
                ->setPath($this->sessionManager->getCookiePath())
                ->setDomain($this->sessionManager->getCookieDomain());

            $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.