2

I need some help on caching issue in CMS block.

I have developed a new product slider for store front that displays latest products in the home page this works great when cache is disabled but when i enable the cache add to cart button is not working, as all the formkey is cached in the block. (P.s. Magento FPC and compilation are enabled)

The widget config as:

{{widget type="catalog/product_widget_new" display_type="all_products" show_pager="1" products_per_page="5000" products_count="5000" template="catalog/product/widget/new/content/new_grid.phtml"}}

Then I include it in the home page like:

<block type="wrapper" name="section.no1">
    <action method="setContainer">
        <container>1</container>
    </action>
    <block type="cms/block" name="best_deals" >
    <action method="setParams">
        <params>class="best_deals"</params>
    </action>
    <action method="setBlockId">
            <block_id>best_deals</block_id>
        </action>
</block>

I have tried..

<action method="setCacheLifetime"><s>0</s></action>

and

<action method="setCacheLifetime"><s>null</s></action>

even..

<action method="unsetData"><key>cache_lifetime</key></action>
<action method="unsetData"><key>cache_tags</key></action>

None of them take effect. Perhaps I'm missing something?

Update: I solved the issue by adding $this->setCacheKey(uniqid()); to my template file.

2 Answers 2

4

You can try in homepage template directly

echo $this->getLayout()->createBlock('cms/block')->setBlockId('block_id')->setCacheKey(uniqid())->toHtml();
2
  • 1
    Thanks for inspiration. I solved the issue by adding $this->setCacheKey(uniqid()); to my template file.
    – Gordon
    Mar 27, 2018 at 6:40
  • For ANONYMOUS_XXX blocks this is a good solution. Fixed an old issue for us, tnx. Feb 21, 2020 at 13:16
0

Fabrizio has a real good write up on why this isn't working for you. Turns out that in XML, when you pass null or even 0 that still counts as a value and not actually null.

http://fbrnc.net/blog/2015/06/cache-and-layout-xml-tricks

His article will explain it better, but I have had the most success with:

class My_Module_Helper_Data extends Mage_Core_Helper_Abstract {

    public function returnNull() 
    {
        return null;
    } 
}

and

<reference name="footer">
    <action method="setCacheLifetime"><lifetime helper="mymodule/returnNull" /></action>
</reference>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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