微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Magento 2 Module setPublicCookie未设置cookie

如何解决Magento 2 Module setPublicCookie未设置cookie

我正在本地的Magento 2中工作。我想在我的自定义创建的模块中设置并获取Cookie。 当通话事件时,我将一些值存储到cookie中。以后我将使用它作为我的要求。 我的密码 events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
     <event name="catalog_product_load_after">
      <observer name="merchanttrack_controller_action_predispatch" instance="Magento\MerchantTrack\Observer\ReceiveAffiliateData" shared="false" />
    </event>
</config>

ReceiveAffiliateData.PHP页面

<?PHP
 
 namespace Magento\MerchantTrack\Observer; 

 use Magento\Framework\Event\ObserverInterface;
 use Magento\Framework\App\Request\DataPersistorInterface;
 use Magento\Framework\App\ObjectManager;
 use Magento\Framework\App\Bootstrap;
 use Psr\Log\LoggerInterface; 
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
use Magento\Framework\Stdlib\Cookie\PublicCookieMetadata;
use Magento\Framework\Stdlib\CookieManagerInterface;


    class ReceiveAffiliateData implements ObserverInterface { 


        protected $logger;
        protected $dir;
        protected $request;
        protected $cookieManager;
        protected $cookieMetadataFactory;

        public function __construct(LoggerInterface $logger,\Magento\Framework\Filesystem\DirectoryList $dir,\Magento\Framework\App\Request\Http $request,\Magento\Framework\Stdlib\CookieManagerInterface $cookieManager,\Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory
        ) 
        { 
        $this->logger = $logger;
        $this->dir = $dir;
        $this->request = $request;
        $this->cookieManager = $cookieManager;
        $this->cookieMetadataFactory = $cookieMetadataFactory;
        }


        public function execute(\Magento\Framework\Event\Observer $observer)
        { 

                $affiliated_id=$this->request->getParam('affiliated_id');
                $coupon_code=$this->request->getParam('coupon_code');
                $discount_type=$this->request->getParam('discount_type');
                $coupon_amount=$this->request->getParam('coupon_amount');
                

                
                if($coupon_code!='' && $discount_type!='' && $coupon_amount!='')
                {

                    $this->setCustomCookie();

                    $this->getCustomCookie();
                }
          }
 public function setCustomCookie()
        {
            $publicCookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata();
            $publicCookieMetadata->setDurationOneYear();
            $publicCookieMetadata->setPath('/');
            $publicCookieMetadata->setHttpOnly(false);
    
            $this->cookieManager->setPublicCookie(
                'magento2cookie','Custom_Cookie_Value',$publicCookieMetadata
            );
        }
    
        /** Get Custom Cookie using */
        public function getCustomCookie()
        {
            $this->cookieManager->getCookie(
                'magento2cookie'
            );
            $this->logger->info($this->cookieManager->getCookie(
                'magento2cookie'
            ));
        }
        
    }

但是不能设置cookie。我不明白我在做什么错。请任何人帮助我。 谢谢。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。