0

I have to set category id in cookie when customer go to Category page and update cookie with category id when visit again using seperate module.

I have created Observer and set cookie there but not working. I have checked Observer is calling and also getting correct category ID.

Issue is with Cache. if Cache is enabled all is working fine.

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_category_init_after">

    <observer name="cookieObserver" instance="Vendor\Module\Observer\Category\Data" />

</event>

</config>

Observer - Data.php

<?php
namespace Vendor\Module\Observer\Category;

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

use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
use Magento\Framework\Stdlib\CookieManagerInterface;


class Data implements ObserverInterface
{

const COOKIE_NAME = 'categoryId';
const COOKIE_DURATION = 86400; // One day (86400 seconds)

protected $cookieManager;

/**
 * @var CookieMetadataFactory
 */
protected $cookieMetadataFactory;

public function __construct(
    CookieManagerInterface $cookieManager,
    CookieMetadataFactory $cookieMetadataFactory
)
{
    $this->cookieManager = $cookieManager;
    $this->cookieMetadataFactory = $cookieMetadataFactory;
}

/**

 * Below is the method that will fire whenever the event runs!

 *

 * @param Observer $observer

 */

public function execute(Observer $observer)
{
    $data = $observer->getData();
    $metadata = $this->cookieMetadataFactory->createPublicCookieMetadata()->setDuration(86400)->setPath('/');
    $this->cookieManager->setPublicCookie('categoryId', $data['category']->getId(), $metadata);

return $this; }

}

Please provide some suggestion or help.

0

You can use event, observer to create/update/delete cookie when some visiting category page.

You can use the event catalog_controller_category_init_after or any other event like layout_load_before.

/* Set cookie */

$categoryId       = 'Category ID'
$objectManager    = \Magento\Framework\App\ObjectManager::getInstance();
$cookieManager    = $objectManager->create('\Magento\Framework\Stdlib\CookieManagerInterface');
$cookieMetadataFactory = $objectManager->create('\Magento\Framework\Stdlib\Cookie\CookieMetadataFactory');
$metadata         = $cookieMetadataFactory
                    ->createPublicCookieMetadata()
                    ->setDuration(1800)
                    ->setPath('/');
$cookieManager->setPublicCookie(
    'categoryId',
    $categoryId,
    $metadata
);


/* Retrive the cookie values */

$cookieManager    = $objectManager->create('\Magento\Framework\Stdlib\CookieManagerInterface');
$cookieValue      = $cookieManager->getCookie('categoryId');

You can follow manual to create a custom extension

  • I have updated question. – Magecode Apr 9 at 10:33
  • @Magecode i have posted the working code, go through your code and debug it. – Rakesh Jakhar Apr 9 at 10:35
  • @Magecode make sure $data['category']->getId() providing the id or you can pass the static vale for now and check in your browser – Rakesh Jakhar Apr 9 at 10:36
  • I have passed static to. – Magecode Apr 9 at 10:42
  • post the browser screenshot which shows cookies – Rakesh Jakhar Apr 9 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.