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;
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
add a comment |
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
add a comment |
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
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
category event-observer magento2.3 cookie
edited yesterday
Magecode
asked 2 days ago
MagecodeMagecode
547421
547421
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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
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
|
show 7 more comments
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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
|
show 7 more comments
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
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
|
show 7 more comments
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
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
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
|
show 7 more comments
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
|
show 7 more comments
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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