Hyvä Theme is Now Open Source: What This Means for Magento Community - Mageplaza
Hyvä is now Open Source and free. Discover what changed, what remains commercial, how it impacts the Magento ecosystem, and how to maximize its full potential.
Vinh Jacker | 03-17-2025
Cookies play a crucial role in Magento 2 by storing user preferences, session data, and tracking information to enhance the customer experience. Whether you’re working on custom functionalities, personalized user experiences, or tracking mechanisms, setting cookies correctly is essential. In this guide, we’ll walk you through how to set, get, and delete cookies in Magento 2 using best practices.
Cookies help improve user experience and store temporary data for various functionalities, such as:
Now, let’s dive into how to set a cookie in Magento 2 using different methods.
The first is setting a Readcookie.php controller in the app/code/Mageplaza/HelloWorld/Controller/Cookie. The Readcookie.php contains the following content:
<?php
namespace Mageplaza\HelloWorld\Controller\Cookie;
class Readcookie extends \Magento\Framework\App\Action\Action
{
/**
* @var \Magento\Framework\Stdlib\CookieManagerInterface
*/
protected $_cookieManager;
/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\Stdlib\CookieManagerInterface $cookieManager
)
{
$this->_cookieManager = $cookieManager;
parent::__construct($context);
}
public function execute()
{
$cookieValue = $this->_cookieManager->getCookie(\Mageplaza\HelloWorld\Controller\Cookie\Addcookie::COOKIE_NAME);
echo($cookieValue);
}
}
Create the Deletecookie controller in the app/code/Mageplaza/HelloWorld/Controller/Cookie with the following content:
<?php
namespace Mageplaza\HelloWorld\Controller\Cookie;
class Deletecookie extends \Magento\Framework\App\Action\Action
{
/**
* @var \Magento\Framework\Stdlib\CookieManagerInterface
*/
protected $_cookieManager;
/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\Stdlib\CookieManagerInterface $cookieManager
)
{
$this->_cookieManager = $cookieManager;
parent::__construct($context);
}
public function execute()
{
$this->_cookieManager->deleteCookie(
\Mageplaza\HelloWorld\Controller\Cookie\Addcookie::COOKIE_NAME
);
echo('DELETED');
}
}

The Cookie Path determines where the cookie applies within your website.
Default: / (applies to all pages).


To ensure cookies are working correctly:
#1. Log into the Admin Panel and navigate to Content → Pages under the Elements section.

#2. Find the Privacy Policy page in the list and click Edit from the Actions dropdown menu. #3. Select the appropriate Store View where the Cookie Policy should be applied, and ensure the page status is set to Enabled. #4. Under the Content tab, update the Cookie Policy details, including:

#5. In the Page in Websites section, confirm the Store View Assignment to control visibility across different stores.

#6. Click Save to apply the changes. #7. Clear the Magento cache under System → Cache Management, then visit the storefront to confirm that the updated Cookie Policy is correctly displayed.

If you’re experiencing issues with Magento 2 cookies, follow these troubleshooting steps to resolve configuration errors and ensure smooth functionality.
php bin/magento cache:clean
php bin/magento cache:flush
1. What are the benefits of configuring cookie files in Magento 2?
Proper cookie configuration in Magento 2 offers several benefits:
2. How do I enable cookies in Magento 2?
Go to Stores → Configuration → Web → Default Cookie Settings, set Enable Cookie Restriction Mode to Yes, and save the configuration.
3. Why is my cookie message not displaying?
Check that Cookie Restriction Mode is enabled under Stores → Configuration → Web. Also, clear the Magento cache and refresh the page.
4. How do I customize the cookie message in Magento 2?
Modify the text in Stores → Configuration → Web → Default Cookie Settings → Cookie Restriction Notice Text.
5. How do I test if cookies are working in Magento 2?
6. How can I disable cookie notifications in Magento 2?
Yes, you can disable the cookie consent banner by turning off Cookie Restriction Mode:
7. Do Magento 2 extensions influence browser cookie settings? Yes, Magento 2 extensions can alter browser cookie settings by adding new cookies or modifying existing ones to improve functionality or analytics. It is recommended to review extension details and their effect on cookies to maintain compliance and ensure a seamless user experience.
Configuring, editing, and deleting cookies in Magento 2 is a simple yet critical step in improving user experience, security, and compliance. By adjusting cookie settings properly, you ensure that session data is stored efficiently while maintaining privacy and security standards.
Related Topics