I need to set a value to the cookie in a phtml file and I need to get it's value in another phtml file.
Is it possible to set and get the values using jQuery?
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityCreate Cookie
We will be creating a test controller to create
app/code/[Name_Space]/[Your_Module]/Controller/Cookie/Addcookie.php
<?php
namespace [Name_Space]\[Your_Module]\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');
}
}
Read Cookie
We will create a Readcookie.php controller to read cookie
app/code/[Name_Space]/[Your_Module]/Controller/Cookie
<?php
namespace [Name_Space]\[Your_Module]\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);
}
}
Delete Cookie
We will create a Deletecookie.php controller to create cookie
app/code/[Name_Space]/[Your_Module]/Controller/Cookie
<?php
namespace [Name_Space]\[Your_Module]\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');
}
}
The 3 steps I mention above is the shortest process for you to Use Cookie in Magento 2. With this guide, you can manage the Cookie in Magento 2 easily.
metadata
you need to specify both path and domain. for example : $this->_cookieMetadataFactory ->createPublicCookieMetadata() ->setDuration(self::COOKIE_DURATION) ->setPath($this->_sessionManager->getCookiePath()) ->setDomain($this->_sessionManager->getCookieDomain());
Jun 17, 2021 at 16:58
<script>
require([
'jquery',
'mage/cookies'
], function ($) {
$.cookie('cookie_name', 'value', { path: '/' });//Set the cookies
});
</script>
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, more discussion about this you could see here Cookie path and its accessibility to subfolder pages.
<script>
require([
'jquery',
'mage/cookies'
], function ($) {
var temp = $.cookie('cookie_name');//Get the cookies
});
</script>
<script>
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>