How to Create/Get/Update/Delete Cookie in Magento 2
In Magento 2 as well as other eCommerce platforms, using cookies is the familiar way to achieve the data. Now, we will dig deeper to understand how to use the cookie in Magento 2.
In Magento 2 as well as other eCommerce platforms, using cookies is the familiar way to achieve the data. Now, we will dig deeper to understand how to use cookies 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?
Overview of using cookie in Magento 2
Step 1: Create a controller to read 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);
}
}
Step 2: Create a controller to delete cookie
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, leave a comment to ask for the help. Good luck to you!
Related Topics
Enjoyed the tutorial? Spread it to your friends!
Sam Thomas
CEO and Founder of Mageplaza. Pursueing a simple and healthy lifestyle. A friend, a husband and a dad of two children, a trainer and an influencer wannabe. He is a big fan of sports and travel, also.
Featured Extensions
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
- 2.2.x, 2.3.x, 2.4.x