我有一个听取<event name="controller_action_predispatch">
事件的观察员。
观察者的执行方法在每个请求上都正确运行,但在启用缓存时没有正确设置Cookie,更具体的是整页缓存。(System -> Cache Management -> Page Cache)
。
现在,当我禁用缓存时,cookie会按预期设置,但是当从产品列表页面向购物车添加商品时,右上角的迷你购物车会保持空白。实际查看迷你购物车中的商品并能够结帐的唯一方法是首先进入随机产品概述页面,然后从那里将其添加到购物车。有什么想法吗?
在观察者中设置Cookies:
$cookieManager->setPublicCookie('Custom_Cookie', 'This is a Cookie');
观察员班:
class MyObserver implements ObserverInterface {
protected $messageManager;
protected $cart;
protected $scopeConfig;
protected $logger;
public function __construct(
\Magento\Framework\Message\ManagerInterface $messageManager,
\Magento\Checkout\Model\Cart $cart,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Psr\Log\LoggerInterface $logger
) {
$this->messageManager = $messageManager;
$this->cart = $cart;
$this->scopeConfig = $scopeConfig;
$this->logger = $logger;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cookieManager = $objectManager->get('Magento\Framework\Stdlib\CookieManagerInterface');
$customer = $objectManager->create('Magento\Customer\Model\Customer');
$product = $objectManager->create('Magento\Catalog\Model\Product');
$cart = $objectManager->get('Magento\Checkout\Model\Cart');
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
// COOKIE
$cookieManager->setPublicCookie('Custom_Cookie', 'This is a Cookie');
$this->logger->info('COOKIE IS ==> ' . $cookieManager->getCookie('Custom_Cookie'));
...
}
答案 0 :(得分:1)
我刚刚建立了一个几乎与你正在做的扩展。请参阅下面我使用的代码。这应该适合你。
观察员类:
<?php
namespace Company\Module\Observer;
class AffiliateTracking implements \Magento\Framework\Event\ObserverInterface
{
/**
* @var \Magento\Framework\Registry
*/
protected $_registry;
/**
* @var \Magento\Framework\App\Request\Http
*/
protected $_request;
/**
* @var
*/
protected $_cookieManager;
/**
* @var CookieMetadataFactory
*/
protected $_cookieMetadataFactory;
protected $_sessionManager;
/**
* The affiliate cookie name
*/
const COOKIE_NAME = "nh_affiliate_id";
public function __construct(
\Magento\Framework\Registry $registry,
\Magento\Framework\App\Request\Http $request,
\Magento\Framework\Stdlib\CookieManagerInterface $cookieManager,
\Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory,
\Magento\Framework\Session\SessionManagerInterface $sessionManager)
{
$this->_registry = $registry;
$this->_request = $request;
$this->_cookieManger = $cookieManager;
$this->_cookieMetadataFactory = $cookieMetadataFactory;
$this->_sessionManager = $sessionManager;
}
/**
* @param \Magento\Framework\Event\Observer $observer
* @return $this
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$affiliateId = $this->_request->getParam("SSAID", false);
if ($affiliateId)
{
$publicCookieMetadata = $this->_cookieMetadataFactory->createPublicCookieMetadata()
->setDuration(2592000)
->setPath($this->sessionManager->getCookiePath())
->setDomain($this->sessionManager->getCookieDomain())
->setHttpOnly(false);
$this->_cookieManager->setPublicCookie(self::COOKIE_NAME,
$affiliateId,
$publicCookieMetadata
);
}
return $this;
}
}