0

Please help me to find the reason that the below added cookie not setting.

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_controller_product_view">
        <observer name="pdp_view_observer" instance="Ts\Catalog\Observer\View" />
    </event>
</config>

Ts\Catalog\Observer\View.php

<?php
namespace Ts\Catalog\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;

class View implements ObserverInterface
{
    /**
     * @param \Ts\Catalog\Helper\ViewAllData $pdpHelper
     */
    public function __construct(
        \Ts\Catalog\Helper\ViewAllData $pdpHelper
    ) {
        $this->pdpHelper = $pdpHelper;
    }

    /**
     * @param Observer $observer
     */
    public function execute(Observer $observer)
    {
        /**
         * Set redirect cookie for google bot
         */
        $this->pdpHelper->setMyCookie();
    }
}

Ts\Catalog\Helper\ViewAllData.php

<?php
namespace Ts\Catalog\Helper;

/**
 * Class ViewAllData
 * @package Ts\Catalog\Helper
 */
class ViewAllData extends \Magento\Framework\App\Helper\AbstractHelper
{
    /**
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
     */
    protected $scopeConfig;

    const VIEW_ALL_ENABLED = 'catalog/frontend/enable_view_all';
    const PRODUCT_VIDEO_AUTOPLAY_ENABLED = 'product_video/general/enable';
    const REDIRECT_COOKIE_LIFETIME = 'catalog/frontend/redirect_cookie_life_time';

    /**
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager
     * @param \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory
     * @param \Magento\Framework\App\Request\Http $request
     */
    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager,
        \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory,
        \Magento\Framework\App\Request\Http $request,
        \Magento\Framework\Session\SessionManagerInterface $sessionManager
    )
    {
        $this->scopeConfig = $scopeConfig;
        $this->storeManager = $storeManager;
        $this->cookieManager = $cookieManager;
        $this->cookieMetadataFactory = $cookieMetadataFactory;
        $this->request = $request;
        $this->sessionManager = $sessionManager;
    }  

 
    /**
     * @param string $cookieName
     * @param string $cookieValue
     * @throws \Magento\Framework\Exception\InputException
     * @throws \Magento\Framework\Stdlib\Cookie\CookieSizeLimitReachedException
     * @throws \Magento\Framework\Stdlib\Cookie\FailureToSendException
     */
    public function setCustomCookie($cookieName = '', $cookieValue = '') {
        $metaData = $this->cookieMetadataFactory->createPublicCookieMetadata();
        $metaData->setDuration(86400)
                 ->setPath($this->sessionManager->getCookiePath())
                 ->setDomain($this->sessionManager->getCookieDomain());
        $this->cookieManager->setPublicCookie(
            $cookieName,
            $cookieValue,
            $metaData
        );
        return $this;
    }


    /**
     * @throws \Magento\Framework\Exception\InputException
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     * @throws \Magento\Framework\Stdlib\Cookie\CookieSizeLimitReachedException
     * @throws \Magento\Framework\Stdlib\Cookie\FailureToSendException
     */
    public function setMyCookie() {
        $this->setCustomCookie('my_cookie',1);
    }
}
1
  • Hello @VIPIN A ROY, did you manage to solve this? Commented Sep 29, 2022 at 10:30

1 Answer 1

0

Use this to solve your issue you missed private variables in class

<?php
namespace Ts\Catalog\Helper;

/**
 * Class ViewAllData
 * @package Ts\Catalog\Helper
 */
class ViewAllData extends \Magento\Framework\App\Helper\AbstractHelper
{
    /**
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
     */
    protected $scopeConfig;
    private $cookieManager;
    private $cookieMetadataFactory;

    const VIEW_ALL_ENABLED = 'catalog/frontend/enable_view_all';
    const PRODUCT_VIDEO_AUTOPLAY_ENABLED = 'product_video/general/enable';
    const REDIRECT_COOKIE_LIFETIME = 'catalog/frontend/redirect_cookie_life_time';

    /**
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager
     * @param \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory
     * @param \Magento\Framework\App\Request\Http $request
     */
    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager,
        \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory,
        \Magento\Framework\App\Request\Http $request,
        \Magento\Framework\Session\SessionManagerInterface $sessionManager
    )
    {
        $this->scopeConfig = $scopeConfig;
        $this->storeManager = $storeManager;
        $this->cookieManager = $cookieManager;
        $this->cookieMetadataFactory = $cookieMetadataFactory;
        $this->request = $request;
        $this->sessionManager = $sessionManager;
    }  


    public function setCustomCookie($a,$b){
        $publicCookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata();
        $publicCookieMetadata->setDurationOneYear();
        $publicCookieMetadata->setPath('/');
        $publicCookieMetadata->setHttpOnly(false);
 
        return $this->cookieManager->setPublicCookie(
            $a,
            $b,
            $publicCookieMetadata
        );
    }

    public function setMyCookie(){
        $this->setCustomCookie('0202','def');
    }
}

Try this and let me know if you face any issue if it works please accept the answer.

2
  • Hi @Pritis, The code successfully working on my local environments and the issue is only on magento cloud environments. Commented Oct 1, 2021 at 13:23
  • Have you checked setcookie function like it works or not ?
    – Prits
    Commented Oct 1, 2021 at 13:29

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.