3

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.

2

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.

1

I have founded a simpler solution, this is the link:

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

Bear in mind that the solution is in the end, in the comments section:

You need go to the layout of your theme and use this link in the specific block:

<action method="unsCacheLifetime"></action>

This solution was given by Daniel Navarro and has worked for me like a charme. This is his explanation: "Did you try to unset the cache lifetime from layout with this?

<action method="unsCacheLifetime"></action>

I feel it should do the work as well because the "uns" deletes the key from the internal _data array of the block... anyway it would be another

For newbies, to find the layout of your theme, open cPanel and go to:

app/design/frontend/YOURTHEME/default/layout/local.xml

I hope that helps!

  • Just to make it clear for newbies... The code above goes under the block line. IE: <block type="cms/block" name="block_product_tab1"> <action method="setBlockId"><block_id>block_product_tab1</block_id></action> <action method="unsCacheLifetime"></action> – Luiz Santos May 27 '17 at 4:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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