Question

I use PHP to create the content of a block dynamically on a CMS page (created via the admin portal). But I found out during testing that the first generated block remains active for all clients until I flush the cache of the server.
My code:

<?php if($_COOKIE["businesskind"] == "private"){

$productsBlock = $this->getLayout()->createBlock(ProductsList::class);
$productsBlock->setProductsCount(100);
$productsBlock->setTemplate("product/widget/content/grid.phtml");
$productsBlock->setConditionsEncoded("^[`1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Combine`,`aggregator`:`all`,`value`:`1`,`new_child`:``^],`1--1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Product`,`attribute`:`category_ids`,`operator`:`==`,`value`:`3`^]^]");
echo $productsBlock->toHtml();
}
else{
$productsBlock = $this->getLayout()->createBlock(ProductsList::class);
$productsBlock->setProductsCount(100);
$productsBlock->setTemplate("product/widget/content/grid.phtml");
$productsBlock->setConditionsEncoded("^[`1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Combine`,`aggregator`:`all`,`value`:`1`,`new_child`:``^],`1--1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Product`,`attribute`:`category_ids`,`operator`:`==`,`value`:`4`^]^]");
echo $productsBlock->toHtml();
}
?>

I create a cookie and query the value of the cookie via PHP. Depending on the input the user should see a different output in the next step. The output in the next step should be a filtered product list. How can the selected one be valid only for the one user?

Was it helpful?

Solution

Sounds like it is being cached in full page cache, I would achieve this by loading in the products via a controller and an AJAX call instead.

Alternatively you could prevent the whole page from being cached by setting cacheable="false" on any block that loads on this page, this isn't advisable because the whole page will never be cached so it will result in poor performance for users.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top