I want to set a few cookies named CUSTOM_NAME_FIRST should be set to customer first name and CUSTOM_NAME_LAST should be set to a customer last name when customers logged in and delete upon customer logout.

I can set the custom cookie using CookieManagerInterface

public function __construct(
    \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager,
    \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory
) {
    $this->cookieManager = $cookieManager;
    $this->cookieMetadataFactory = $cookieMetadataFactory;

/** Set Custom Cookie using Magento 2 */
public function setCustomCookie()
    $publicCookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata();

    return $this->cookieManager->setPublicCookie(

But can someone show me better way using js or section?

1 Answer 1


try this link

How utilize Cookie Storage in Magento 2



  • 1
    links may not work in the future, it is more helpful to post an answer rather than a link to info.
    – paj
    Feb 28, 2020 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.