custom_cookie_magento_2 custom_cookie_magento_2

How to Create, Read, Update and Delete Custom Cookie in Magento 2

Do you want to know how to create, read, update and delete the custom cookie in Magento 2? A cookie is very important for a PHP application. As such, Magento developer often needs a cookie to be store in the client browser to track and read particular behaviour of the visitor.

Today we will see how to create, read, update and delete the cookie in Magento 2 using PHP code and using JS code.

In this example, I’m showing how to manage cookie in observer class. You can use this code snippet in any of your custom module class.

<?php
namespace CodextblogCustomcookieObserverFrontendCustomer;

use MagentoFrameworkStdlibCookieCookieMetadataFactory;
use MagentoFrameworkStdlibCookieManagerInterface;
use MagentoStoreApiDataStoreInterface;

class Makemycookie implements MagentoFrameworkEventObserverInterface
{
const COOKIE_NAME = 'CUSTOMER_GROUP_ID';

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

public function execute(
        Observer $observer
    ) {
        $store = $this->store;
        $customer = $observer->getEvent()->getCustomer();
        /*CREATE OR UPDATE COOKIE*/
        $this->setStoreCookie($store, $customer);
        /*READ COOKIE*/
        $this->getStoreCookie();
        /*DELETE COOKIE*/
        $this->deleteStoreCookie($store);
      }
public function setStoreCookie($store, $customer)
    {
        $cookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()
            ->setHttpOnly(true)
            ->setDurationOneYear()
            ->setPath('/');

        $this->cookieManager->setPublicCookie(self::COOKIE_NAME, $customer->getGroupId(), $cookieMetadata);
    }

public function getStoreCookie()
    {
        return $this->cookieManager->getCookie(self::COOKIE_NAME);
    }

public function deleteStoreCookie($store)
    {
        $cookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()
            ->setPath($store->getStorePath());

        $this->cookieManager->deleteCookie(self::COOKIE_NAME, $cookieMetadata);
    }
}

Create one custom js file and call that file in your phtml template. In one of the article we have already seen how to call custom js file in Magento 2.


define([
    'jquery',
    'mage/cookies'
], function ($) {
/** create cookie */

    $.cookie('CUSTOMER_GROUP_ID', "1", { expires: 7, path: '/' });

/** update cookie */

    $.cookie("CUSTOMER_GROUP_ID", "2");

/** read cookie */

    $.cookie('CUSTOMER_GROUP_ID');

/** delete cookie */

    $.cookie("CUSTOMER_GROUP_ID", null);

});
Written by

Chirag

Chirag is a Founder & eCommerce Technology Leader at MageArmy. He leads the eCommerce development team and drives the company’s technology vision, helping businesses build, scale, and optimize high-performing online stores. With years of experience in eCommerce development, he has worked with businesses across industries to solve complex technical challenges and create scalable digital commerce solutions. He loves sharing practical insights about eCommerce development, Magento, Adobe Commerce, and the latest trends in digital commerce to help online businesses grow and succeed.