Set cookie in Observer Not working when full page Cache is enabled The 2019 Stack Overflow Developer Survey Results Are InMagento Cookie Value Not SetMagento 2 cookies not working when all cache is enabledSet custom price of product when adding to cart code not workingFull page cache issue with cookie in M2What is the recommanded cookie lifetime when persistent cart enabled?Cookie restriction mode not workingDelete custom cookie in logout observer not workingMagento 2: After custom cookie is created all pages default to home pageMagento 2.3 Can't view module's front end page output?MsrpPriceCalculator Exception

What does ひと匙 mean in this manga and has it been used colloquially?

For what reasons would an animal species NOT cross a *horizontal* land bridge?

Is "plugging out" electronic devices an American expression?

Can one be advised by a professor who is very far away?

What could be the right powersource for 15 seconds lifespan disposable giant chainsaw?

Is bread bad for ducks?

Distributing a matrix

Origin of "cooter" meaning "vagina"

Loose spokes after only a few rides

Did 3000BC Egyptians use meteoric iron weapons?

Can you compress metal and what would be the consequences?

What is the most effective way of iterating a std::vector and why?

Have you ever entered Singapore using a different passport or name?

Protecting Dualbooting Windows from dangerous code (like rm -rf)

Aging parents with no investments

Why is the Constellation's nose gear so long?

Why can Shazam fly?

Looking for Correct Greek Translation for Heraclitus

"as much details as you can remember"

Resizing object distorts it (Illustrator CC 2018)

One word riddle: Vowel in the middle

Is flight data recorder erased after every flight?

What do hard-Brexiteers want with respect to the Irish border?

Delete all lines which don't have n characters before delimiter



Set cookie in Observer Not working when full page Cache is enabled



The 2019 Stack Overflow Developer Survey Results Are InMagento Cookie Value Not SetMagento 2 cookies not working when all cache is enabledSet custom price of product when adding to cart code not workingFull page cache issue with cookie in M2What is the recommanded cookie lifetime when persistent cart enabled?Cookie restriction mode not workingDelete custom cookie in logout observer not workingMagento 2: After custom cookie is created all pages default to home pageMagento 2.3 Can't view module's front end page output?MsrpPriceCalculator Exception



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








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="VendorModuleObserverCategoryData" />

</event>

</config>



Observer - Data.php




<?php
namespace VendorModuleObserverCategory;

use MagentoFrameworkEventObserver;
use MagentoFrameworkEventObserverInterface;

use MagentoFrameworkStdlibCookieCookieMetadataFactory;
use MagentoFrameworkStdlibCookieManagerInterface;


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.










share|improve this question






























    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="VendorModuleObserverCategoryData" />

    </event>

    </config>



    Observer - Data.php




    <?php
    namespace VendorModuleObserverCategory;

    use MagentoFrameworkEventObserver;
    use MagentoFrameworkEventObserverInterface;

    use MagentoFrameworkStdlibCookieCookieMetadataFactory;
    use MagentoFrameworkStdlibCookieManagerInterface;


    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.










    share|improve this question


























      0












      0








      0


      1






      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="VendorModuleObserverCategoryData" />

      </event>

      </config>



      Observer - Data.php




      <?php
      namespace VendorModuleObserverCategory;

      use MagentoFrameworkEventObserver;
      use MagentoFrameworkEventObserverInterface;

      use MagentoFrameworkStdlibCookieCookieMetadataFactory;
      use MagentoFrameworkStdlibCookieManagerInterface;


      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.










      share|improve this question
















      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="VendorModuleObserverCategoryData" />

      </event>

      </config>



      Observer - Data.php




      <?php
      namespace VendorModuleObserverCategory;

      use MagentoFrameworkEventObserver;
      use MagentoFrameworkEventObserverInterface;

      use MagentoFrameworkStdlibCookieCookieMetadataFactory;
      use MagentoFrameworkStdlibCookieManagerInterface;


      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.







      category event-observer magento2.3 cookie






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited yesterday







      Magecode

















      asked 2 days ago









      MagecodeMagecode

      547421




      547421




















          1 Answer
          1






          active

          oldest

          votes


















          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 = MagentoFrameworkAppObjectManager::getInstance();
          $cookieManager = $objectManager->create('MagentoFrameworkStdlibCookieManagerInterface');
          $cookieMetadataFactory = $objectManager->create('MagentoFrameworkStdlibCookieCookieMetadataFactory');
          $metadata = $cookieMetadataFactory
          ->createPublicCookieMetadata()
          ->setDuration(1800)
          ->setPath('/');
          $cookieManager->setPublicCookie(
          'categoryId',
          $categoryId,
          $metadata
          );


          /* Retrive the cookie values */

          $cookieManager = $objectManager->create('MagentoFrameworkStdlibCookieManagerInterface');
          $cookieValue = $cookieManager->getCookie('categoryId');


          You can follow manual to create a custom extension






          share|improve this answer























          • I have updated question.

            – Magecode
            yesterday











          • @Magecode i have posted the working code, go through your code and debug it.

            – Rakesh Jakhar
            yesterday











          • @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
            yesterday











          • I have passed static to.

            – Magecode
            yesterday











          • post the browser screenshot which shows cookies

            – Rakesh Jakhar
            yesterday











          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "479"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f269294%2fset-cookie-in-observer-not-working-when-full-page-cache-is-enabled%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          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 = MagentoFrameworkAppObjectManager::getInstance();
          $cookieManager = $objectManager->create('MagentoFrameworkStdlibCookieManagerInterface');
          $cookieMetadataFactory = $objectManager->create('MagentoFrameworkStdlibCookieCookieMetadataFactory');
          $metadata = $cookieMetadataFactory
          ->createPublicCookieMetadata()
          ->setDuration(1800)
          ->setPath('/');
          $cookieManager->setPublicCookie(
          'categoryId',
          $categoryId,
          $metadata
          );


          /* Retrive the cookie values */

          $cookieManager = $objectManager->create('MagentoFrameworkStdlibCookieManagerInterface');
          $cookieValue = $cookieManager->getCookie('categoryId');


          You can follow manual to create a custom extension






          share|improve this answer























          • I have updated question.

            – Magecode
            yesterday











          • @Magecode i have posted the working code, go through your code and debug it.

            – Rakesh Jakhar
            yesterday











          • @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
            yesterday











          • I have passed static to.

            – Magecode
            yesterday











          • post the browser screenshot which shows cookies

            – Rakesh Jakhar
            yesterday















          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 = MagentoFrameworkAppObjectManager::getInstance();
          $cookieManager = $objectManager->create('MagentoFrameworkStdlibCookieManagerInterface');
          $cookieMetadataFactory = $objectManager->create('MagentoFrameworkStdlibCookieCookieMetadataFactory');
          $metadata = $cookieMetadataFactory
          ->createPublicCookieMetadata()
          ->setDuration(1800)
          ->setPath('/');
          $cookieManager->setPublicCookie(
          'categoryId',
          $categoryId,
          $metadata
          );


          /* Retrive the cookie values */

          $cookieManager = $objectManager->create('MagentoFrameworkStdlibCookieManagerInterface');
          $cookieValue = $cookieManager->getCookie('categoryId');


          You can follow manual to create a custom extension






          share|improve this answer























          • I have updated question.

            – Magecode
            yesterday











          • @Magecode i have posted the working code, go through your code and debug it.

            – Rakesh Jakhar
            yesterday











          • @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
            yesterday











          • I have passed static to.

            – Magecode
            yesterday











          • post the browser screenshot which shows cookies

            – Rakesh Jakhar
            yesterday













          0












          0








          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 = MagentoFrameworkAppObjectManager::getInstance();
          $cookieManager = $objectManager->create('MagentoFrameworkStdlibCookieManagerInterface');
          $cookieMetadataFactory = $objectManager->create('MagentoFrameworkStdlibCookieCookieMetadataFactory');
          $metadata = $cookieMetadataFactory
          ->createPublicCookieMetadata()
          ->setDuration(1800)
          ->setPath('/');
          $cookieManager->setPublicCookie(
          'categoryId',
          $categoryId,
          $metadata
          );


          /* Retrive the cookie values */

          $cookieManager = $objectManager->create('MagentoFrameworkStdlibCookieManagerInterface');
          $cookieValue = $cookieManager->getCookie('categoryId');


          You can follow manual to create a custom extension






          share|improve this answer













          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 = MagentoFrameworkAppObjectManager::getInstance();
          $cookieManager = $objectManager->create('MagentoFrameworkStdlibCookieManagerInterface');
          $cookieMetadataFactory = $objectManager->create('MagentoFrameworkStdlibCookieCookieMetadataFactory');
          $metadata = $cookieMetadataFactory
          ->createPublicCookieMetadata()
          ->setDuration(1800)
          ->setPath('/');
          $cookieManager->setPublicCookie(
          'categoryId',
          $categoryId,
          $metadata
          );


          /* Retrive the cookie values */

          $cookieManager = $objectManager->create('MagentoFrameworkStdlibCookieManagerInterface');
          $cookieValue = $cookieManager->getCookie('categoryId');


          You can follow manual to create a custom extension







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday









          Rakesh JakharRakesh Jakhar

          1866




          1866












          • I have updated question.

            – Magecode
            yesterday











          • @Magecode i have posted the working code, go through your code and debug it.

            – Rakesh Jakhar
            yesterday











          • @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
            yesterday











          • I have passed static to.

            – Magecode
            yesterday











          • post the browser screenshot which shows cookies

            – Rakesh Jakhar
            yesterday

















          • I have updated question.

            – Magecode
            yesterday











          • @Magecode i have posted the working code, go through your code and debug it.

            – Rakesh Jakhar
            yesterday











          • @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
            yesterday











          • I have passed static to.

            – Magecode
            yesterday











          • post the browser screenshot which shows cookies

            – Rakesh Jakhar
            yesterday
















          I have updated question.

          – Magecode
          yesterday





          I have updated question.

          – Magecode
          yesterday













          @Magecode i have posted the working code, go through your code and debug it.

          – Rakesh Jakhar
          yesterday





          @Magecode i have posted the working code, go through your code and debug it.

          – Rakesh Jakhar
          yesterday













          @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
          yesterday





          @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
          yesterday













          I have passed static to.

          – Magecode
          yesterday





          I have passed static to.

          – Magecode
          yesterday













          post the browser screenshot which shows cookies

          – Rakesh Jakhar
          yesterday





          post the browser screenshot which shows cookies

          – Rakesh Jakhar
          yesterday

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Magento Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f269294%2fset-cookie-in-observer-not-working-when-full-page-cache-is-enabled%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

          Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

          Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form