I'm strugling with disabling the cache for an individual block in Magento 1.9. I've enabled the "HTML Blocks" in cache management and the site is much faster although I have a static block on the product page that stays the same when it should refresh depending on the supplier of the product.
The Static block is called block_product_secondary_bottom
and in that block is the following:
<p class="no-margin ">{{block type="core/template" name="supplier.delivery" template="myphp/delivery.phtml"}}</p>
myphp/delivery.phtml
contains the following code – basically it looks up info from a table:
<?php
$product_id = Mage::registry('current_product')->getId();
$attributeValue = Mage::getModel('catalog/product')
->load($product_id)
->getAttributeText('advertiser_name');
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
$query = "Select * from `smashingmagazine_branddirectory_brand` Where `name`='".$attributeValue."'";
$rows = $connection->fetchAll($query);
$output = 0;
foreach ($rows as $values) {
$name = $attributeValue;
$about = $values['about_supplier'];
$discounts = $values['description'];
$image = $values['url_key'];
}
if (!empty($name)) {
$output = '<h6 class="block-title heading" style="text-align: center; font-weight:bold;">About '.$name.'</h6>
<div class="block-content">';
};
if (!empty($image)) {
$output.= '<p class="no-margin "style="text-align: center;"><img src='.$image.'></p>';
};
if (!empty($about)) {
$output.= '<p class="no-margin ">'.$about.'</p>';
};
if (!empty($discounts)) {
$output.= '<div class="feature feature-icon-hover indent first" style="padding-top:15px;">
<span class="ib ic ic-lg ic-plane"></span>
<p class="no-margin ">'.$discounts.'</p></div>';
};
$output.= "</div>";
echo $output;
I've tried adding the following code into my local.xml
, clearing Cache and refreshing – but this doesn't work:
<block name="supplier.delivery" template="myphp/delivery.phtml" type="core/template">
<action method="unsetData">
<key>cache_lifetime</key>
</action>
<action method="unsetData">
<key>cache_tags</key>
</action>
</block>
I wondered if anyone could tell me how I could fix this issue? Either by adding actions to the local.xml
or by adding PHP to the .phtml
file.
Best Answer
Please see this excellent article:
http://fbrnc.net/blog/2015/06/cache-and-layout-xml-tricks
For completeness:
create a helper class:
and set the value in your layout:
an alternative, but not so elegant a solution is to use your own derived block class.
and then derive your block from this, not core/template
{{block type="my_module/nocache" name="supplier.delivery" template="myphp/delivery.phtml"}}you can define your own cache tags in the construct:
Note that there can be a potential that the parent block is also cached, which would then in turn cache your block there. Subsequent page requests would not even reach your block, as the parent(s) cached output would simply be used.