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