How to Create/Get/Update/Delete Cookie in Magento 2

In Magento 2 as well as other ecommerce platforms, using cookie is the familiar way to achieve the data. Now, we will dig deeper to understand how to use the cookie in Magento 2.

What are Cookies?

Cookies is a type of document that is used to save all information on the browser. Namely, when a user visits your website and leaves some personal info, the cookies allows showing those data if he is back in the next time. With the cookies, you may be confident to enhance the customer shopping experience.

So, what do you need to follow in order to use the cookies in Magento 2?

  • Step 1: Create a controller to read cookie
  • Step 2: Create a controller to delete cookie

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');
}
}

And now, you can enable the cookie on your Magento 2 store. If you have any trouble in tracking the topic, please contact us to ask for the help. Good luck to you!



Next tutorial:

Module Development Series



Enjoyed the tutorial? Spread it to your friends!

magento-2-tutorial
how-to
use
cookie

Comments for How to Create/Get/Update/Delete Cookie in Magento 2

Please leave comments if you have any questions, feedbacks.

Module Development

You also may like these Magento 2 Extensions

One Step Checkout

$199
10 reviews

Layered Navigation

$99
12 reviews

SEO

FREE
no review

Shop By Brand

$99
3 reviews

People also searched for:

  • how to manage cookie magento 2
  • how to create cookie magento 2
  • how to update cookie magento 2
  • how to delete cookie magento 2
  • how to read cookie magento 2
  • magento 2 cookies
  • magento 2 set cookie javascript
  • magento 2 cookie domain
  • magento 2 set session variable
  • magento set cookie javascript
  • magento 2 customer session
  • get cookie in magento 2
  • set cookie path in magento 2
  • create cookie in magento 2
  • how to create cookie in magento 2
  • how to get cookie in magento 2