Cookie in magento 2
This tutorial is about Use or Set cookie in magento 2. i.e how to set or use cookie in magento 2?. Suppose you want to set a value to the cookie in a phtml file and need to get it’s value in another phtml file.
There are many ways in magento 2.I will try to discuss some in this tutorial.
Certainly magento uses jquery and it is possible to set and get the values using jQuery.
First of all lets have a look at our first setp. i.e
Set the cookies
You can set the cookies using below code:
require([
'jquery',
'mage/cookies'
], function ($) {
$.cookie('cookie_name', 'value', { path: '/' });//Set the cookies
});
</script>
Please note that if you want to get the value from another page, notice the path setting { path: ‘/’ }!
Because cookies are only accessible to the specified path and any subpaths.
Similarly to get cookies:
Get Cookies
require([
'jquery',
'mage/cookies'
], function ($) {
var temp = $.cookie('cookie_name');//Get the cookies
});
</script>
Similarly another piece of code is as below:
require(
[ 'jquery'],function($){
//set cookie
$.cookie('customname', 'customvalue', { expires: 365, path: '/' });
//get cookie
getCookieValue(customname)
});
function getCookieValue(cookiename) {
var name = cookiename + "=";
var cookieSplit = document.cookie.split(';');
for(var i = 0; i <cookieSplit.length; i++) {
var a = cookieSplit[i];
while (a.charAt(0)==' ') {
a = a.substring(1);
}
if (a.indexOf(name) == 0) {
return a.substring(name.length,a.length);
}
}
return "";
}
</script>
Furthermore there are other way too to do so. I am going to discuss them below. Wil try to keep it short and precise.
Create Cookie
Create a test controller to create:
app/code/QaisarSatti/HelloWorld/Controller/Cookie/Addcookie.php
namespace QaisarSatti\HelloWorld\Controller\Cookie;
class Addcookie extends \Magento\Framework\App\Action\Action
{
const COOKIE_NAME = 'test';
const COOKIE_DURATION = 86400; // lifetime in seconds
/**
* @var \Magento\Framework\Stdlib\CookieManagerInterface
*/
protected $_cookieManager;
/**
* @var \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory
*/
protected $_cookieMetadataFactory;
/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager
* @param \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\Stdlib\CookieManagerInterface $cookieManager,
\Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory
)
{
$this->_cookieManager = $cookieManager;
$this->_cookieMetadataFactory = $cookieMetadataFactory;
parent::__construct($context);
}
public function execute()
{
$metadata = $this->_cookieMetadataFactory
->createPublicCookieMetadata()
->setDuration(self::COOKIE_DURATION);
$this->_cookieManager->setPublicCookie(
self::COOKIE_NAME,
'YOUR COOKIE VALUE',
$metadata
);
echo('Create Cookie');
}
}
Similarly to Read cookie
Read Cookie
Create a Readcookie.php controller to read cookie
app/code/QaisarSatti/HelloWorld/Controller/Cookie
namespace QaisarSatti\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(\[Name_Space]\[Your_Module]\Controller\Cookie\Addcookie::COOKIE_NAME);
echo($cookieValue);
}
}
Furthermore Delete Cookie.
Delete Cookie
Create a Deletecookie.php controller to create cookie.
namespace QaisarSatti\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(
\[Name_Space]\[Your_Module]\Controller\Cookie\Addcookie::COOKIE_NAME
);
echo('DELETED');
}
}
Hence above mentioned steps are the shortest process for you to Use Cookie in Magento 2. I have tried to provide you the code with simple logic.
That’s it from this tutorial. I hope it serves the purpose.
Since these are learning tutorials,please feel free to drop any suggestions or queries in comments section. That will definitely be highly appreciated.