Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

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

Vinh Jacker | 12-18-2024

Create/Update/Delete Cookie

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?


Magento 2 extensions

Magento 2 extensions

Allow you to achieve more with your online store

Check it out!


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, leave a comment to ask for the help. Good luck to you!

Related Topics

x
    Jacker

    With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.



    Related Post

    Website Support
    & Maintenance Services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.

    Get Started
    mageplaza services