Back to Top

Set, get and delete data from cookie in magento 2

Updated 30 December 2025

Set get and delete cookies in Magento 2 is a common requirement for storing small data such as user preferences, session values, or tracking information.

In this article, you will learn how to set get and delete cookies in Magento 2 using a custom module and Magento’s cookie management classes.


Create a Cookie Handler Class in Magento 2

To set get and delete cookies in Magento 2, first create a Cookie folder inside your module directory: app/code/Namespace/Module/Cookie/

Then create a Custom.php file. This class will handle all cookie operations in Magento 2.


Magento 2 Cookie Management Class Example

The following example shows how to manage cookies in Magento 2 using CookieManagerInterface and CookieMetadataFactory.

Searching for an experienced
Magento 2 Company ?
Find out More
<?php
namespace Namespace\Module\Cookie;

use Magento\Framework\Session\SessionManagerInterface;
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
use Magento\Framework\Stdlib\CookieManagerInterface;

class Custom
{
    public const COOKIE_NAME = "webkul_cookie";

    protected $_cookieManager;
    protected $_cookieMetadataFactory;
    protected $_sessionManager;

    public function __construct(
        CookieManagerInterface $cookieManager,
        CookieMetadataFactory $cookieMetadataFactory,
        SessionManagerInterface $sessionManager
    ) {
        $this->_cookieManager = $cookieManager;
        $this->_cookieMetadataFactory = $cookieMetadataFactory;
        $this->_sessionManager = $sessionManager;
    }

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

    public function set($value, $duration = 86400)
    {
        $metadata = $this->_cookieMetadataFactory
            ->createPublicCookieMetadata()
            ->setDuration($duration)
            ->setPath($this->_sessionManager->getCookiePath())
            ->setDomain($this->_sessionManager->getCookieDomain());

        $this->_cookieManager->setPublicCookie(
            self::COOKIE_NAME,
            $value,
            $metadata
        );
    }

    public function delete()
    {
        $this->_cookieManager->deleteCookie(
            self::COOKIE_NAME,
            $this->_cookieMetadataFactory
                ->createCookieMetadata()
                ->setPath($this->_sessionManager->getCookiePath())
                ->setDomain($this->_sessionManager->getCookieDomain())
        );
    }
}

How to Set Cookies in Magento 2

To set cookies in Magento 2, call the set() method from your custom cookie class.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$objectManager->get(
   'Namespace\Module\Cookie\Custom'
)->set('custom_data', 3600);

This stores the cookie value for one hour.


How to Get Cookies in Magento 2

To get cookies in Magento 2, use the get() method.

$value = $objectManager->get(
   'Namespace\Module\Cookie\Custom'
)->get();

echo $value;

This retrieves the stored cookie value.


How to Delete Cookies in Magento 2

To delete cookies in Magento 2, call the delete() method.

$objectManager->get(
   'Namespace\Module\Cookie\Custom'
)->delete();

This removes the cookie from the browser.


Best Practices for Cookies in Magento 2

Always use Magento’s cookie management APIs. This ensures security, proper scope handling, and compatibility with Magento core updates.


Conclusion

Using this approach, you can safely manage cookies without relying on native PHP cookie functions. This method is clean, secure, and fully aligned with Magento development standards.

If you need technical assistance, please reach out to us at [email protected]. Additionally, discover various solutions to improve your online store by visiting the Adobe Commerce modules section.

For professional advice or to create custom functionalities, consider hiring Adobe Commerce Developers for your project.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


2 comments

  • Rafael Corrêa Gomes ♛
  • Franzim Barruk
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home