How To

How to Set and Get Cookie in Magento 2?

Hello Magento Friends,

In today’s blog, we will learn about How to Set and Get Cookie in Magento 2?

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 Vendor\Extension\Model;

class Customcookie
{
    private $customCookieManager;

    private $customCookieMetadataFactory;

    public function __construct(
        \Magento\Framework\Stdlib\CookieManagerInterface $customCookieManager,
        \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $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'
        );
    }
}

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. 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. 

Happy Coding!

Click to rate this post!
[Total: 5 Average: 4]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

2 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

2 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

3 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

4 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

6 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

6 days ago