1

I want to delete cookies programmatically before as login customer.

1
  • Above url will help you Nov 16, 2018 at 10:13

2 Answers 2

3

Overview of using cookie in Magento 2 Step 1: Create a controller to read cookie Step 2: Create a controller to delete cookie Step 1: Create a controller to read cookie 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);
}
}

Step 2: Create a controller to delete cookie 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!

4
2

The short answer could look like this.

if ($this->_cookieManager->getCookie('recently_viewed_product')) {
    $metadata = $this->_cookieMetadataFactory->createCookieMetadata()
        ->setPath($this->_sessionManager->getCookiePath());

    $this->_cookieManager->deleteCookie('recently_viewed_product', $metadata);
}

Be sure to include the following interfaces, factories and etc.

use Magento\Framework\Session\SessionManagerInterface;
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
use Magento\Framework\Stdlib\CookieManagerInterface;

....

protected $_sessionManager;
protected $_cookieMetadataFactory;
protected $_cookieManager;

....

public function __construct(
    ....
    Context $context,
    SessionManagerInterface $sessionManager,
    CookieManagerInterface $cookieManager,
    CookieMetadataFactory $cookieMetadataFactory
) {
    $this->_sessionManager = $sessionManager;
    $this->_cookieManager = $cookieManager;
    $this->_cookieMetadataFactory = $cookieMetadataFactory;

    parent::__construct($context);
}

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.