'Magento 2.4 cookie not setting via observer code

I am trying 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);
    }
}


Solution 1:[1]

I have had a problem like this previously, most times you may have that same card linked to several google services in which one of them has a pending bill to pay.

OR

There have been errors when a google service has tried charging this card and it failed.

OR

You have an outstanding payment (which is very unlikely to cause an error)

What I did when I had this problem

"I USED ANOTHER CARD"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Timileyin