3

I have cookies already set on the browser, name as cookie_name. And how can I use magento 2 cookies manager. to retrieve the cookie already set on the browser.

<?php

 namespace YourCompany\Module\Plugin;

 use Magento\Framework\Exception\LocalizedException;

class CartPlugin
{

/**
 * @var \Magento\Quote\Model\Quote
 */
protected $quote;

protected $request;
/**
* @var \Magento\Framework\Stdlib\CookieManagerInterface
*/
protected $_cookieManager;
/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager
*/
 static protected $_key = null;
/**
 * Plugin constructor.
 *
 * @param \Magento\Checkout\Model\Session $checkoutSession
 */
public function __construct(
    \Magento\Checkout\Model\Session $checkoutSession,
    \Magento\Framework\App\Request\Http $request, 
    \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager
) {
    $this->_cookieManager = $cookieManager;
    $this->quote = $checkoutSession->getQuote();
    $this->request = $request;        
}

/**
 * beforeAddProduct
 *
 * @param      $subject
 * @param      $productInfo
 * @param null $requestInfo
 *
 * @return array
 * @throws LocalizedException
 */

 public function beforeAddProduct($subject, $productInfo, $requestInfo = null)
{        
    $cookieValue = $this->_cookieManager->getCookie('cookie_name');
    var_dump($cookieValue);
    die();
    return [$productInfo, $requestInfo];
}

The result on browser is given me NULL

2 Answers 2

7

You can try the below code to get cookie.

<?php

namespace Training\Rememberme\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
use Magento\Framework\Stdlib\CookieManagerInterface;
use Magento\Framework\Session\SessionManagerInterface;


class Data extends AbstractHelper
{

    /**
     * Name of Cookie that holds private content version
     */
    CONST COOKIE_NAME = 'remember';

    /**
     * Cookie life time
     */
    CONST COOKIE_LIFE = 604800;

    /**
     * @var \Magento\Framework\Stdlib\CookieManagerInterface
     */
    protected $cookieManager;

    /**
     * @var \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory
     */
    protected $cookieMetadataFactory;

    /**
     * @var $scopeConfigInterface
     */
    private $scopeConfigInterface;

    /**
     * @var \Magento\Framework\Session\SessionManagerInterface
     */
    protected $sessionManager;


    public function __construct(
        ScopeConfigInterface $scopeConfigInterface,
        CookieManagerInterface $cookieManager,
        CookieMetadataFactory $cookieMetadataFactory,
        SessionManagerInterface $sessionManager
    ){
        $this->scopeConfigInterface = $scopeConfigInterface;
        $this->cookieManager = $cookieManager;
        $this->cookieMetadataFactory = $cookieMetadataFactory;
        $this->sessionManager = $sessionManager;
    }

    /**
     * Get data from cookie set in remote address
     *
     * @return value
     */
    public function getCookie($name)
    {
        return $this->cookieManager->getCookie($name);
    }

    /**
     * Set data to cookie in remote address
     *
     * @param [string] $value    [value of cookie]
     * @param integer $duration [duration for cookie] 7 Days
     *
     * @return void
     */
    public function setCookie($value, $duration = 604800)
    {
        $metadata = $this->cookieMetadataFactory
            ->createPublicCookieMetadata()
            ->setDuration($duration)
            ->setPath($this->sessionManager->getCookiePath())
            ->setDomain($this->sessionManager->getCookieDomain());

        $this->cookieManager->setPublicCookie(self::COOKIE_NAME, $value, $metadata);

    }

    /**
     * delete cookie remote address
     *
     * @return void
     */
    public function delete($name)
    {
        $this->cookieManager->deleteCookie(
            $name,
            $this->cookieMetadataFactory
                ->createCookieMetadata()
                ->setPath($this->sessionManager->getCookiePath())
                ->setDomain($this->sessionManager->getCookieDomain())
        );
    }

    /**
     * @return var
     */
    public function getCookielifetime()
    {
        return self::COOKIE_LIFE;
    }
}

Call the Helper in your file like

$_helper = $this->helper('Training\Rememberme\Helper\Data');

Then call the function like below.

$_helper->getCookie('cookiename');

1
  • 1
    Still give me null
    – codePhree
    Commented Jul 12, 2017 at 11:56
1

Here is how I delete. I'm sure you can modify to get.

public function execute(
    \Magento\Framework\Event\Observer $observer
) {
    $cookieMetadata = $this->_cookieMetadataFactory->createCookieMetadata()->setPath('/');
    $this->_cookieManager->deleteCookie('you-cookie-name', $cookieMetadata);
}
1
  • What is the use of "->setPath('/')" Commented Nov 20, 2019 at 13:26

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.