---
title: "How to Set and Get Cookie in Magento 2?"
url: "https://magecomp.com/blog/set-and-get-cookie-magento-2/"
date: "2022-09-26T13:38:49+00:00"
modified: "2025-02-14T06:06:10+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 236
reading_time: "2 min read"
summary: "Hello Magento Friends,"
description: "Cookies are used to monitor users. Easily set and get cookie in Magento 2. Learn more"
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "7 Best Magento 2 AI ChatGPT Extension Providers in 2026"
    url: "https://magecomp.com/blog/best-magento-2-ai-chatgpt-extension-providers/"
  - title: "How to Create a Directory Programmatically in Magento 2?"
    url: "https://magecomp.com/blog/create-directory-programmatically-magento-2/"
  - title: "How to Get Product Salable Quantity in Magento 2?"
    url: "https://magecomp.com/blog/get-product-salable-quantity-magento-2/"
---

# How to Set and Get Cookie in Magento 2?

_Published: September 26, 2022_  
_Author: Dhiren Vasoya_  

![How to Set and Get Cookie in Magento 2](https://magecomp.com/blog/wp-content/uploads/2022/09/How-to-Set-and-Get-Cookie-in-Magento-2-1024x512.webp)

**Hello Magento Friends,**

In today’s blog, we will learn about How to Set and Get Cookie in Magento 2?
[![Magento Malware Removal](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-Malware-Removal-CTA-3.webp)](https://magecomp.com/magento-malware-removal.html)

Cookies are small files on a user’s computer that are usually used to identify a user. Cookies are used to track users and provide a personalized experience. You can set, update, and retrieve cookie. In Magento 2 you can easily set and get cookie data in Magento 2.
## Steps to Set and Get Cookie in Magento 2:
**Step 1:** Create a **Customcookie.php** file at the below path
**app\code\Vendor\Extension\Model**

And add the code as follows
```
<?php
namespace VendorExtensionModel;

class Customcookie
{
    private $customCookieManager;

    private $customCookieMetadataFactory;

    public function __construct(
        MagentoFrameworkStdlibCookieManagerInterface $customCookieManager,
        MagentoFrameworkStdlibCookieCookieMetadataFactory $customCookieMetadataFactory)
   {
        $this->customCookieManager = $customCookieManager;
        $this->customCookieMetadataFactory = $customCookieMetadataFactory;
    }

    public function setCookie()
    {
        $customCookieMetadata = $this->customCookieMetadataFactory->createPublicCookieMetadata();
        $customCookieMetadata->setDurationOneYear();
        $customCookieMetadata->setPath('/');
        $customCookieMetadata->setHttpOnly(false);

        return $this->customCookieManager->setPublicCookie(
            'customcookie',
            'Cookie_Value',
            $customCookieMetadata
        );
    }

    public function getCookie()
    {
        return $this->customCookieManager->getCookie(
            'customcookie'
        );
    }
}
```

## [![Magento 2 Speed & Performance Optimization Services](https://magecomp.com/blog/wp-content/uploads/2023/10/Magento-2-Speed-Performance-Optimization-Services-CTA-4.webp)](https://magecomp.com/magento-performance-optimization.html)

## Conclusion:
Using the above method, you can easily set and get cookie data in Magento 2. Inform your users if your site uses cookies and take consent from them with the help of [**Cookie Compliance Extension for Magento 2**](https://magecomp.com/magento-2-cookie-compliance.html). If you have any doubts, share them with me through the comment section. Also, share the article with your friends to help them work with cookie values.
[![hyva theme development](https://magecomp.com/blog/wp-content/uploads/2025/02/hyva-theme-development-CTA-3.webp)](https://magecomp.com/services/hyva-theme-development/)

**Happy Coding!**


---

_View the original post at: [https://magecomp.com/blog/set-and-get-cookie-magento-2/](https://magecomp.com/blog/set-and-get-cookie-magento-2/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-11-02 17:58:27 UTC_  
