78

I have a website with multiple static blocks which was working in 1.9.1.0, but with 1.9.2.0 the static blocks start displaying sporadically, as they sometimes show the wrong block rather than the correct block. Sometimes they display as desired. Does anyone know how to solve this issue which may be related to this issue?

14
  • This sounds unreproducable and crazy. If you have a better explanation we might help you, but so I see no way, sorry. Commented Jul 8, 2015 at 16:04
  • 2
    I can confirm this. Encountered it on one shop. Cache keys are identical between the 2 blocks Commented Jul 8, 2015 at 16:53
  • 8
    There is an open bug for this magentocommerce.com/bug-tracking/issue/index/id/870 Commented Jul 8, 2015 at 19:00
  • 9
    According to Piotr this is now a confirmed bug and is being looked into by Magento core team.
    – benmarks
    Commented Jul 9, 2015 at 15:07
  • 1
    This bug is still apparent in 1.9.2.4. Added scenario/details to bug tracker comments.
    – zigojacko
    Commented Aug 9, 2016 at 15:48

13 Answers 13

62

I had this problem with EE 1.14.2 and it looks like the same issue has come up in CE 1.9.2. I documented my problem and solution on this SE question.

Basically 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);

CMS static blocks are now cached. The problem arises from how the cache key info is generated. It falls back to the Mage_Core_Block_Abstract behavior of using the blocks name in layout. If the block hasn't been added with layout, e.g on a cms page, this name doesn't exist. This can result in static blocks sharing the same cache key and getting mixed up in cache.

My solution was to override the Mage_Cms_Block_Block class and set the cache key info based on the block id and current store.

/**
 * Override cms/block to add cache key. This started being a problem as of EE 1.14.2 and CE 1.9.2 when the _construct
 * method was added which turns on caching for cms blocks
 */
class Mysite_Cms_Block_Block extends Mage_Cms_Block_Block
{

    /**
     * If this block has a block id, use that as the cache key.
     *
     * @return array
     */
    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();
        }
    }
}

Obviously this would need to be added in your own module with a config.xml file and block override etc. Alternatively you could copy Mage_Cms_Block_Block to the local code pool and add the cache key there.

You can see the new lines added in 1.9.2 here

9
  • How are you instantiating these static blocks? If you do it through layout xml, you can just add a unique name to your block and it should cache fine. In which scenario would you not give a name to your block (widgets perhaps?)
    – Erfan
    Commented Jul 9, 2015 at 3:36
  • 1
    @Erfan This problem happens when adding blocks to CMS pages like this {{block type="cms/block" block_id="block_id"}} or when loading a static block directly in a template Commented Jul 9, 2015 at 4:13
  • 6
    here is a free extension for this wierd problem. Hope it will benefit everyone who face this problem. github.com/progammer-rkt/Rkt_SbCache Commented Jul 9, 2015 at 9:36
  • @Rajeev Which versions of Magento is it compatible with? Will also work correct with Mage 1.6.x?
    – zitix
    Commented Jul 14, 2015 at 16:57
  • 1
    I created a gist for a temporary solution out of andrewkett's answer. Just add this app/code/local/Mage/Cms/Block/Block.php to your Magento directory and the problem will be solved. Just remember to remove the file before you perform your next Magento update (assuming they include the fix in the next update). Commented Jul 21, 2015 at 5:03
13

I just upgraded to 1.9.2.0 and I'm experiencing this too. Categories that are set to display Static Blocks + Products are randomly displaying the wrong Static Block. This did not exist prior to my 1.9.2.0 upgrade.

Temporary Fix Disable the Blocks HTML output cache and the blocks displayed are correctly.

3
  • Im facing this also at the home page and in the product pages
    – w.k
    Commented Jul 8, 2015 at 16:18
  • This work for now but should be a bitter solution
    – w.k
    Commented Jul 8, 2015 at 16:49
  • Yep, this is what I did for now.
    – Sharif
    Commented Jul 14, 2015 at 14:22
13

Here we go with local module based solution as above solution does not provide whole steps. We need to create Custom module because you all know that Magento Boogieman will get you! if change the core :)

You will need to following files: app/etc/modules/Bhupendra_Cms.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Bhupendra_Cms>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Cms/>
            </depends>
        </Bhupendra_Cms>
    </modules>
</config>

app/code/local/Bhupendra/Cms/etc/config.xml

<?xml version="1.0"?>
<config>
        <modules>
            <Bhupendra_Cms>
                <version>1.0.0</version>
            </Bhupendra_Cms>
        </modules>
        <global>
            <blocks>
                <cms>
                    <rewrite>
                        <block>Bhupendra_Cms_Block_Block</block>
                        <widget_block>Bhupendra_Cms_Block_Widget_Block</widget_block>
                    </rewrite>
                </cms>
            </blocks>
        </global>
</config>

app/code/local/Bhupendra/Cms/Block/Block.php

<?php
class Bhupendra_Cms_Block_Block extends Mage_Cms_Block_Block {

   public function getCacheKeyInfo()
    {

      $blockId = $this->getBlockId();
      if ($blockId) {
            $result = array(
                $blockId,
                Mage::app()->getStore()->getCode(),
            );
      } else {
           $result = parent::getCacheKeyInfo();
       }
       return $result;
   }

}

app/code/local/Bhupendra/Cms/Block/Widget/Block.php

class Bhupendra_Cms_Block_Widget_Block extends Mage_Cms_Block_Widget_Block
{
       /**
     * Storage for used widgets
     *
     * @var array
     */
    static protected $_widgetUsageMap = array();

    /**
     * Prepare block text and determine whether block output enabled or not
     * Prevent blocks recursion if needed
     *
     * @return Mage_Cms_Block_Widget_Block
     */
    protected function _beforeToHtml()
    {
        parent::_beforeToHtml();
        $blockId = $this->getData('block_id');
        $blockHash = get_class($this) . $blockId;

        if (isset(self::$_widgetUsageMap[$blockHash])) {
            return $this;
        }
        self::$_widgetUsageMap[$blockHash] = true;

        if ($blockId) {
            $block = Mage::getModel('cms/block')
                ->setStoreId(Mage::app()->getStore()->getId())
                ->load($blockId);
            if ($block->getIsActive()) {
                /* @var $helper Mage_Cms_Helper_Data */
                $helper = Mage::helper('cms');
                $processor = $helper->getBlockTemplateProcessor();
                $this->setText($processor->filter($block->getContent()));
                $this->addModelTags($block);
            }
        }

        unset(self::$_widgetUsageMap[$blockHash]);
        return $this;
    }

     /**
     * Retrieve values of properties that unambiguously identify unique content
     *
     * @return array
     */
    public function getCacheKeyInfo()
    {
        $result = parent::getCacheKeyInfo();
        $blockId = $this->getBlockId();
        if ($blockId) {
            $result[] = $blockId;
       }
        return $result;
   }
}

For more information you can visit following link and also download from it https://www.milople.com/blogs/ecommerce/solved-magento-static-block-display-issue.html

4
  • Setup the module and it didn't help - my issue is that CMS blocks assigned to category disappear, they don't show at all.
    – Haim
    Commented Mar 20, 2016 at 14:51
  • @Haim if you are using 1.9.2.2 then you need make entry into System->permission - > cms block of that particular block Commented Mar 21, 2016 at 7:12
  • i'm using Magento ver. 1.9.2.0 - but again my issue is slightly different, I set a CMS block to show on a category page and the CMS block just doesn't show - if i refresh the cache it shows until the next time it just dissapears
    – Haim
    Commented Mar 21, 2016 at 16:17
  • I suggest you to update magento version i think it has been now. Commented Mar 22, 2016 at 6:15
7

There is no official patch for this, however, it was resolved in CE 1.9.2.1

diff -r magento-CE-1.9.2.0/app/code/core/Mage/Cms/Block/Block.php magento-CE-1.9.2.1/app/code/core/Mage/Cms/Block/Block.php
74a75,94
> 
>     /**
>      * Retrieve values of properties that unambiguously identify unique content
>      *
>      * @return array
>      */
>     public function getCacheKeyInfo()
>     {
>         $blockId = $this->getBlockId();
>         if ($blockId) {
>             $result = array(
>                 'CMS_BLOCK',
>                 $blockId,
>                 Mage::app()->getStore()->getCode(),
>             );
>         } else {
>             $result = parent::getCacheKeyInfo();
>         }
>         return $result;
>     }
diff -r magento-CE-1.9.2.0/app/code/core/Mage/Cms/Block/Widget/Block.php magento-CE-1.9.2.1/app/code/core/Mage/Cms/Block/Widget/Block.php
84a85
>                 $this->addModelTags($block);
90a92,106
> 
>     /**
>      * Retrieve values of properties that unambiguously identify unique content
>      *
>      * @return array
>      */
>     public function getCacheKeyInfo()
>     {
>         $result = parent::getCacheKeyInfo();
>         $blockId = $this->getBlockId();
>         if ($blockId) {
>             $result[] = $blockId;
>         }
>         return $result;
>     }

Note: It's been reported that there are still issues with CMS pages on multiple store views:

Magento CE 1.9.2.1 fixes this only partially.

The problem still persists for CMS Pages on multiple store views. Here is an Updated Hotfix (Note this is not an official patch): https://gist.github.com/tux-rampage/77b286f7973336877f7b#file-luka-mce20150805-1-9-2-1-caching-hotfix-patch

Source: http://www.magentocommerce.com/products/bug-tracking/issue/index/id/870

1
4

I can confirm this issue too.

To reproduce:

  1. Using the CMS -> Widgets, create a widget to add a static block to the left sidebar.

  2. Then create second widget to add second static block (different block than in step 1) to the left sidebar.

  3. If cache is disabled, both static blocks are correctly displayed in the sidebar.

  4. But if you enable cache, you will see the static block from step 1 displayed twice.

1
  • Hi Zitix I face the same issue like you can you tell me how you solve this
    – Jaimin
    Commented Aug 23, 2017 at 6:53
3

Piotr from Magento has published a non-official patch for this issue for the time being: https://gist.github.com/piotrekkaminski/ecd245e8c9390e4020db

Seems to do the trick. Yes, it's editing the core, but fixes the issue until Magento releases the official patch or next version...

1
  • This trick is used on the newer version 1.9.3.3. however it doesnt fix my issue.
    – Rebel
    Commented Jul 17, 2017 at 22:03
3

Upgrade Magento to 1.9.2.1

I did it and enabled cache for Blocks HTML output and it seems to be fixed.

As well as some security issues are addressed in new release.

3

In latest version of Magento enhances it with new security features . you can add permissions in system->permissions to static block.

3
  • 2
    in magento 1.9.2.0 there is a problem in caching static block , you are talking about something different.
    – w.k
    Commented Nov 12, 2015 at 15:20
  • Nice , I was not aware of this feature Commented Mar 20, 2016 at 15:22
  • I like this. :) Commented Jan 16, 2017 at 7:13
2

I had the same issue with my shop. The best workaround I have found out so far is to deactivate caching for the affected blocks. You can do so by setting the cache lifetime of the blocks to null.

Globally disabling the "Blocks HTML output" cache on a live site is not a good idea as it unnecessarily impacts site performance.

Disabling the cache for one block in xml:

<block ... >
    ...
    <action method="unsetData"><key>cache_lifetime</key></action>
    <action method="unsetData"><key>cache_tags</key></action>
</block>

Disabling the cache for one block in php:

$this->getLayout()->createBlock('cms/block')
    ->setCacheLifetime(null)
    ->setBlockId('block-id')
    ->toHtml();

Do not set the cache lifetime to "0" as pointed out in this article

4
  • Andreas - I'm curious what is the impact of using the technique described in that article and what how do the two alternatives you provide here avoid the negative consequences you outline? Commented Jul 21, 2015 at 20:59
  • 1
    the first approach is not going to work because, magento will interpret that layout update as lik this $block->setCacheLifeTime("null"); Note NULL and "null" are two different things (later one is a string) and hence expected result will not obtain. Commented Jul 22, 2015 at 12:39
  • 1
    @BJ Hoffpauir: If the cache for a cms/block block is activated, the same cached cms/block will be used for all store views. If you have an english(store view) and german(store view) version for the same block-identifier either the english or the german one will be used for both store views. Disabling the cache for this block solves the problem. If you still want to cache the block you can do so by putting the block into another block and cache this block instead. Commented Jul 23, 2015 at 9:18
  • 1
    @Rajeev Thanks, I found this method in several posts, but I am pretty sure you are right. I changed the method to disable the cache in xml. The previous method was an accepted answer here though: stackoverflow.com/questions/27684236/… Commented Jul 23, 2015 at 9:24
2

I was able to fix this issue by updating the existing extensions in Magento Connection Manager. After looking into it, I was under the impression that the problem exists in the Magento caching system.

By default Magento has several packages related to its caching technologies. They include adapters and libraries for Zend and Redis.

Instead of trying to find the appropriate package, I chose to update all the packages in my installation.

Then I chose the checkmark on: Mage_All_Latest Which is a Metapackage for the latest stable Magento 1.9.0.0 release.

It may be possible to fix the issue by upgrading only the right packages. I felt this was the better way to go because I am under suspicion that this method also applies security patches.

1

You will have to do a full upgrade, or backport 1.9.2.0

CMS Block and Widget caching changes in 1.9.2.1

magento-1921/app/code/core/Mage/Cms/Block/Block.php

diff -r magento-1920/app/code/core/Mage/Cms/Block/Block.php magento-1921/app/code/core/Mage/Cms/Block/Block.php
74a75,94
> 
>     /**
>      * Retrieve values of properties that unambiguously identify unique content
>      *
>      * @return array
>      */
>     public function getCacheKeyInfo()
>     {
>         $blockId = $this->getBlockId();
>         if ($blockId) {
>             $result = array(
>                 'CMS_BLOCK',
>                 $blockId,
>                 Mage::app()->getStore()->getCode(),
>             );
>         } else {
>             $result = parent::getCacheKeyInfo();
>         }
>         return $result;
>     }

magento-1921/app/code/core/Mage/Cms/Block/Widget/Block.php

diff -r magento-1920/app/code/core/Mage/Cms/Block/Widget/Block.php magento-1921/app/code/core/Mage/Cms/Block/Widget/Block.php
84a85
>                 $this->addModelTags($block);
89a91,105
>     }
> 
>     /**
>      * Retrieve values of properties that unambiguously identify unique content
>      *
>      * @return array
>      */
>     public function getCacheKeyInfo()
>     {
>         $result = parent::getCacheKeyInfo();
>         $blockId = $this->getBlockId();
>         if ($blockId) {
>             $result[] = $blockId;
>         }
>         return $result;
0

I'm using Magento 1.9.3.8 and the issue still exists.

You can find my fix here:

Basically I'm adding a unique string based on page url and blockId to each cache key info, so each block will have a unique key:

 /**
 * Generates a string based on the page url (for example category/product pages) and concatenate the block id to the url
 * Removes the caracters: /, . , &, = and , from this string
 */
private function generateUrlBasedString($blockId = null)
{
    $currentUrl = Mage::helper('core/url')->getCurrentUrl();
    $url = Mage::getSingleton('core/url')->parseUrl($currentUrl);
    $path = '_' . $url->getPath();

    $path = str_replace('/', '', $path);
    $path = str_replace('.', '', $path);
    $path = str_replace('&', '', $path);
    $path = str_replace(',', '', $path);
    $path = str_replace('=', '', $path);

    if(isset($blockId)) {
        $path .= '_' . $blockId;
    }

    return $path;
}
/**
 * Retrieve values of properties that unambiguously identify unique content
 *
 * @return array
 */
public function getCacheKeyInfo()
{
    $blockId = $this->getBlockId();
    if ($blockId) {
        $result = array(
            'CMS_BLOCK',
            $blockId,
            Mage::app()->getStore()->getCode() . $this->generateUrlBasedString($blockId),
        );
    } else {
        $result = parent::getCacheKeyInfo();
    }
    return $result;
}

Until Magento will prepare a fix for this issue, you can create the file:

app/code/local/Mage/Cms/Block/Block.php

and insert the code from the above github url as content.

This code is tested for Magento 1.9.2.* and 1.9.3.*

-1

This is confirmed bug in 1.9.2 version, for time being you can resolve this problem by just disabling "Blocks HTML output" cache from admin -> cache management section

Hope it helps

3
  • studio2f had mention thie answer, u can use @andrewkett or click on "this issue" in the main question this will help u also
    – w.k
    Commented Jul 16, 2015 at 8:28
  • 2
    I would say that disabling all of block caching to address a problem that only manifests itself on some specific blocks is a bit like chopping off one's entire hand if you develop frostbite on a finger. Chopping off the finger, while also unpleasant, would at least be a more proportionate response to the malady. Andrewkett's (magento.stackexchange.com/users/527/andrewkett) answer is a more reasonable approach: magento.stackexchange.com/questions/73685/… Commented Jul 21, 2015 at 22:13
  • Never edit or change CORE classes. Commented Sep 14, 2016 at 10:11

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