Magento Tips & Tricks

Magento 1.9.2.0 Static blocks issues

Recently we have updated our demo site to 1.9.2 and notice strange behaviour of static blocks. Magento was showing wrong static blocks.
After some research it turns out that it is known issue and moreover, there is an open bug for this magentocommerce.com/bug-tracking/issue/index/id/870.

This happens due to the following code being added to the constructor of Mage_Cms_Block_Block:

$this->setCacheTags(array(Mage_Cms_Model_Block::CACHE_TAG));
$this->setCacheLifetime(false);

It adds cache functionality to static blocks. But cache key info is generated in Mage_Core_Block_Abstract, which use block name in layout. Block name does not exist since static block are not added though layout, and as a result they have the same cache key.

To fix it we have created simple extension. It override Mage_Cms_Block_Block to generate correct cache key info for each static block based on store id, block id and secure connection.
Source code on Github

  1. Create /app/etc/modules/Nwdthemes_Cmsfix.xml
    <?xml version="1.0"?>
    <config>
        <modules>
            <Nwdthemes_Cmsfix>
                <active>true</active>
                <codePool>local</codePool>
                <depends>
                    <Mage_Cms/>
                </depends>
            </Nwdthemes_Cmsfix>
        </modules>
    </config>
  2. Create /app/code/local/Nwdthemes/Cmsfix/etc/config.xml
    <?xml version="1.0"?>
    <config>
    	<modules>
    		<Nwdthemes_Cmsfix>
    			<version>1.0.0</version>
    		</Nwdthemes_Cmsfix>
    	</modules>
    	<global>
    		<blocks>
    			<cms>
    				<rewrite>
    					<block>Nwdthemes_Cmsfix_Block_Block</block>
    				</rewrite>
    			</cms>
    		</blocks>
    	</global>
    </config>
  3. Create /app/code/local/Nwdthemes/Cmsfix/Block/Block.php
    <?php
    class Nwdthemes_Cmsfix_Block_Block extends Mage_Cms_Block_Block {
    
        public function getCacheKeyInfo()
        {
            if ($this->getBlockId()) {
                return array(
                    Mage_Cms_Model_Block::CACHE_TAG,
                    Mage::app()->getStore()->getId(),
                    $this->getBlockId(),
                    (int) Mage::app()->getStore()->isCurrentlySecure()
                );
            } else {
                return parent::getCacheKeyInfo();
            }
        }
    }
  4. Thats it. CMS static blocks issue solved.
    Source code on Github

Leave a Reply

Your email address will not be published. Required fields are marked *