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?

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

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

Your Answer

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

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