Join over 10,000 ecommerce enthusiasts who receive free updates
Get email updates
Build A "Smarter"
Magento Store
Magik Extra Fees Create unlimited number & types of extra fees and charges to drive increased revenue per order.
$199  $99

Most popular posts

How To Turn Off Cache For An Individual Block In Magento

 

Have you ever felt a need where one or many of your individual blocks shouldn’t be cached by Magento automatically? We are talking about any individual block and not the entire “block html” caching.

Lets me first tell you how Magento works. Magento uses construct functions within their block classes, which ensures that your store’s head, header and footer and any child blocks are cached automatically. This is a great feature of Magento which ensures every block of module is cached unless specified otherwise.

While doing an extension we realized that we don’t want a particular block to be cached at all due to architectural and design requirements. After banging our head for several hours we came up with two solutions:

1. Turn off Block HTML cache entirely (we will not disable entire caching but only disable bock html caching). We took this approach as that was the easiest (at least we thought it was best and easiest way to do this). But what about other blocks which requires caching. We dug deeper and came up with another option.

2. Turn off block caching while declaring the XML block. This is how it should be done, check the following code which is straightforward in explaining what it does.

In your layout.xml file declare your block like this which needs no caching.

1
2
3
4
5
6
7
<catalog_product_view>
   <reference name="footer">
     <action method="unsetData"><key>cache_lifetime</key></action>
     <action method="unsetData"><key>cache_tags</key></action>
    <block type="mymodule/mymodule_block" name="module_name" as="module_name_alias" template="mymodule/uncached_block.phtml" after="-" />
   </reference>
</catalog_product_view>

Please note that you should change your module declaration, block name and template file in this line

1
 <block type="mymodule/mymodule_block" name="module_name" as="module_name_alias" template="mymodule/uncached_block.phtml" after="-" />

Now, some explanation: unsetData tags in Magento are pretty helpful and you can build you logic around them. First you need to ensure that unsetData tag exist in your reference (footer, header etc.). If yes, you can use them to tell Magento not to cache a particular block on a desired page. This way you can easily make any custom block dynamic.

I hope this will help you turn off caching for an individual block without messing up the code. Please let us a comment and let us konw your thoughts on how to turn off caching for an individual block in Magento.

 
Enjoyed this Post?
Then you can follow us here:

Subscribe to RSS
Follow us on Twitter
Follow us on Facebook

Ashish Nayyar

Chief Product Officer & Architect. MagikCommerce.com

Build A "Smarter" Magento Store
Magik Extra Fees Magik Extra Fees is the #1 extention for creating unlimited number & types of extra fees and charges to drive increased revenue per order.
  • Extra Fee for Products
  • Extra Fee for Categories
  • Extra Fee for Shipping
  • Multiple Additional Charges
$199  $99