How to set and get cookie in Magento 2?

Magento 2 You can add/set cookies and get/retrieve the cookie using Magento\Framework\Stdlib\CookieManagerInterface Interface.

By default, You can set the cookie using $_COOKIE with native PHP.

When you are following the coding standard in Magento 2, PHPMd displays the error for $_COOKIE is discouraged in Magento 2.

One Reply to “How to set and get cookie in Magento 2?”

  1. Rajesh I am not a spam , I am developer who is asking for the help.

    let’s say we have executes below code at one single request,
    setCustomCookie(); // cookie has been set
    echo getCustomCookie(); // This will print nothing at first request. what if I want to get value of cookie here?

Comments are closed.