Magento – Remove Custom Block from Cache

cachefull-page-cachemagento-1.9static-block

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:

class My_Module_Helper_Data extends Mage_Core_Helper_Abstract {

    public function returnNull() 
    {
        return null;
    } 
}

and set the value in your layout:

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

an alternative, but not so elegant a solution is to use your own derived block class.

class My_Module_Block_NoCache extends Mage_Core_Block_Template {
protected function _construct()
    {
        $this->addData(array('cache_lifetime' => null));
    }
}

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:

$this->addCacheTag(array(
                Mage_Core_Model_Store::CACHE_TAG,
                "MY_OWN_CACHE_TAG"
            ));

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.

Related Topic