1

How to set uniqid in cookie in Magento2?

 public function generateVisitorId()
    {
         $visitorId = $this->_checkoutSession->get('visitor_id');              
        if (!$visitorId) {
            $visitorId = uniqid();
         $this->_checkoutSession->set('visitor_id', $visitorId, $this->getExpiryTime(), '/');        
        }
        return $visitorId;
    }

But id returning only empty array .If any changes in code....If anyone knows please expain me....

2
  • Also trying $this->_cookieManager->setCookie('visitor_id', $visitorId, $this->getExpiryTime(), '/');
    – Mahi M
    Commented Dec 11, 2017 at 5:30
  • But its returning Call to protected method Magento\Framework\Stdlib\Cookie\PhpCookieManager::setCookie() Error
    – Mahi M
    Commented Dec 11, 2017 at 5:30

2 Answers 2

0

Find the code below,

  <?php

namespace Namespace\Modulename\Controller\Index;
use Magento\Framework\Session\SessionManagerInterface;
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
use Magento\Framework\Stdlib\Cookie\PublicCookieMetadata;
use Magento\Framework\Stdlib\CookieManagerInterface;


class Classname extends \Magento\Framework\App\Action\Action
{
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Registry $registry,CookieManagerInterface $cookieManager,
        CookieMetadataFactory $cookieMetadataFactory,
        SessionManagerInterface $sessionManager
        )
    {
        $this->registry = $registry;

        $this->_cookieManager = $cookieManager;
        $this->_cookieMetadataFactory = $cookieMetadataFactory;
        $this->_sessionManager = $sessionManager;


        return parent::__construct($context);
    }

    public function execute()
    {

        $seller_id = $this->getRequest()->getParam('seller_id');



        $this->registry->register('seller_id',$seller_id);


        //$this->deleteCookie(); this is for delete the cookie
        $this->setCookie();
        return true    
    }


    public function deleteCookie()
    {

        $duration = '86400';

        $this->_cookieManager->deleteCookie(
            'seller_id',
            $this->_cookieMetadataFactory
                ->createCookieMetadata()
                ->setPath($this->_sessionManager->getCookiePath())
                ->setDomain($this->_sessionManager->getCookieDomain())
        );
        return true;

    }

    public function setCookie()
    {

         $duration = '86400';

         $metadata = $this->_cookieMetadataFactory
            ->createPublicCookieMetadata()
            ->setDuration($duration)
            ->setPath($this->_sessionManager->getCookiePath())
            ->setDomain($this->_sessionManager->getCookieDomain());

        $this->_cookieManager->setPublicCookie(
            'seller_id',
            $this->getSellerId(),
            $metadata
        );

        return true;

    }



    public function getSellerName()
    {
        return $this->registry->registry('seller_id');
    }


}
1
  • not working while FPC is enabled. Commented Aug 13, 2018 at 14:25
0

Try to use the class Magento\Framework\Stdlib\CookieManagerInterface;

and the function setPublicCookie() to set a cookie

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.