2

I want to place a specified Static Block in my category page on my Magento site. The code should look up if a specifically named Static Block exists, and if so display the content of it. I do this on a product page absolutely fine, but it appears that the Block Cache in a category breaks it.

I am using the code

 echo $this->getLayout()->createBlock('cms/block')->setBlockId("$categoryBlock")->toHtml();

Where $categoryBlock is a generated string in the form category_bluewidget or category_redwidget. In the Static Blocks I have set up separate blocks called category_bluewidget, category_redwidget etc.

The first page I visit the code works perfectly, however the block appears to be cached and the rest of the pages show the same CMS block on them, not the one that should show.

If I go to admin and turn off Blocks HTML output it works fine, I don't want to do that though as it will slow the site down terriibly. I am looking at just stopping this Static Block cache.

  • Should have said, I am using catalog/category/view.phtml in my custom theme to place the code into – bateman_ap Jul 7 '15 at 21:46
0

There is one function getCmsBlockHtml in Magento\Catalog\Block\Category\View.php

Kindly override that class using custom module and put below code in that file:

 public function getCmsBlockHtml()
    {
        if (!$this->getData('cms_block_html')) {
            $html = $this->getLayout()->createBlock(
                'Magento\Cms\Block\Block'
            )->setBlockId(
                $this->getCurrentCategory()->getLandingPage()
            )->setCacheLifetime(null)->toHtml();
            $this->setData('cms_block_html', $html);
        }
        return $this->getData('cms_block_html');
    }

Hope this helps....!

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.