How to Set and Get Data from The Cookie?

3

In this blog, we will see how to set and get data from the cookie in Magento2.

Please check the below steps, here you will get the idea of how we can use the cookie?

Step 1: First, need to create controller which set the data into cookie

Create file in the following path app/code/Yournamespace/Yourmodule/Controller/Index/Savedata.php
<?php
namespace Yournamespace\Yourmodule\Controller\Index;

class Savedata extends \Magento\Framework\App\Action\Action {
	const COOKIE_NAME = 'custom_cookie_name';
	const COOKIE_DURATION = 86400; 

	protected $_cookieManager;
	protected $_cookieMetadataFactory;

	public function __construct(
		\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)->setPath('/');
		$this->_cookieManager->setPublicCookie(self::COOKIE_NAME,'custom_cookie_value',$metadata);        
	}
}
?>

In the above code, I have used the setPublicCookie() function that is used to store data in the cookie with time duration.

also, cookie will unset its value after that given time duration. 

PHP also provides the superglobal variable $_COOKIE but it’s not the standard way, we should have to follow the Magento standards that’s why we will use Magento\Framework\Stdlib\CookieManagerInterface Interface.

Step 2: Now create the block file which get the value of cookie

Create file in the following path app/code/Yournamespace/Yourmodule/Block/Cookie.php

<?php
namespace Yournamespace\Yourmodule\Block;

class Cookie extends BaseBlock {
	const COOKIE_NAME = 'custom_cookie_name';
	protected $_cookieManager;

	public function __construct(
		Context $context,
		\Magento\Framework\Stdlib\CookieManagerInterface $cookieManager            
	) {
		$this->_cookieManager = $cookieManager;        
		parent::__construct($context);
	}
	
	public function getCookieData() {
		$result = $this->_cookieManager->getCookie(self::COOKIE_NAME);
		return $result;
	}
}

Step 3: Now, you can see how you will call the function in the phtml file

$jsonData = array();
$getCookieData = $block->getCookieData();    //It will call our block which return cookie value
if($getCookieData){
  $jsonData = json_decode($getCookieData);
}

Here, you can see we have called the getCookieData() function which returns the value of the cookie.

Thanks for reading this post.

About the author

I’m Magento Certified Developer having quite 5 years of commercial development expertise in Magento as well as in Shopify. I’ve worked primarily with the Magento and Shopify e-commerce platform, managing the complexities concerned in building e-commerce solutions tailored to a client’s specific desires.

Related Posts

Leave a Reply