1

I have created one helper file that get, set and delete cookie. This helper file I called in observer to set cookie. The helper file called but cookie not set when full cache enabled.

<?php

namespace Hs\Mageenhancement\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\Stdlib\CookieManagerInterface;
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
use Magento\Framework\Session\SessionManagerInterface;

class Cookie extends AbstractHelper
{   
    protected $_storeManager;
    protected $_cookieManager;
    protected $_cookieMetadataFactory;
    protected $_sessionManager;

    public function __construct(
        Context $context,
        StoreManagerInterface $storeManager,
        CookieManagerInterface $cookieManager,
        CookieMetadataFactory $cookieMetadataFactory,
        SessionManagerInterface $sessionManager
    )
    {
        parent::__construct($context);
        $this->_storeManager = $storeManager;
        $this->_cookieManager = $cookieManager;
        $this->_cookieMetadataFactory = $cookieMetadataFactory;
        $this->_sessionManager = $sessionManager;
    }

    /**
     * Get data from cookie
     *
     * @return value
     */
    public function get($name)
    {
        return $this->_cookieManager->getCookie($name);
    }

    /**
     * Set data to cookie in remote address
     *
     * @param [string] $name    [name of cookie]
     * @param [string] $value    [value of cookie]
     * @param integer  $duration [duration for cookie] [Default value 30 days]
     *
     * @return void
     */
    public function set($name, $value, $duration = 2592000)
    {
        //echo $name . ' ' . $value . ' ' . $this->_sessionManager->getCookiePath() . ' ' . $this->_sessionManager->getCookieDomain();
        $metadata = $this->_cookieMetadataFactory
            ->createPublicCookieMetadata()
            ->setDuration($duration)
            ->setPath($this->_sessionManager->getCookiePath())
            ->setDomain($this->_sessionManager->getCookieDomain());

        $this->_cookieManager->setPublicCookie(
            $name,
            $value,
            $metadata
        );
    }
}

Is there any mistake or any other way to set a cookie with full cache module?

2
  • what observer are you using?
    – Marius
    Commented May 2, 2019 at 6:41
  • @Marius <event name="controller_action_predispatch"> <observer name="mageenhancement_tracking_settings" instance="Hs\Mageenhancement\Observer\TrackingObserver" /> </event> observer use.
    – Hiren Shah
    Commented May 2, 2019 at 6:42

2 Answers 2

1

The page cache process method in vendor/magento/framework/App/PageCache/Kernel.php clears the set-cookie header

You'll need to find an event that fires after this to set a cookie

0

Thanks, Chirs Rutledge,

This solution is successfully implemented after consulting Chirs Rutledge's answer.

Create observer for event controller_front_send_response_before

File Your_Module/etc/frontend/events.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="controller_front_send_response_before">
        <observer name="set_member_email_cookie" instance="Your\Module\Observer\ControllerFrontSendResponseObserver" />
    </event>
</config>

File app/code/Your/Module/Observer/ControllerFrontSendResponseObserver.php

<?php
namespace Your\Module\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Session\Config\ConfigInterface;
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
use Magento\Framework\Stdlib\CookieManagerInterface;

class ControllerFrontSendResponseObserver implements ObserverInterface
{
    /**
     * @var CookieManagerInterface
     */
    protected $cookieManager;

    /**
     * @var CookieMetadataFactory
     */
    protected $cookieMetadataFactory;

    /**
     * @var ConfigInterface
     */
    private $sessionConfig;

    public function __construct(
        CookieManagerInterface $cookieManager,
        CookieMetadataFactory $cookieMetadataFactory,
        ConfigInterface $sessionConfig
    ) {
        $this->cookieManager = $cookieManager;
        $this->cookieMetadataFactory = $cookieMetadataFactory;
        $this->sessionConfig = $sessionConfig;
    }

    public function execute(Observer $observer)
    {
        $metadata = $this->cookieMetadataFactory->createPublicCookieMetadata()
            ->setSameSite('Lax')
            ->setPath($this->sessionConfig->getCookiePath())
            ->setDomain($this->sessionConfig->getCookieDomain())
            ->setDuration($this->sessionConfig->getCookieLifetime())
            ->setSecure(false)
            ->setHttpOnly(false);

        $this->cookieManager->setPublicCookie(
            'key',
            'value',
            $metadata
        );
    }
}

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.