2

The goal is to call a static cms block at the bottom of the category page, with block identifier == category name (so that I can make a block in the admin backend for every category).

So I first thought of just adding a static cms block in the catalog_category.xml in my theme, but I think it is not possible to call the category name inside of the xml (or is it?)

Second try was to call a phtml template in the xml file. In the phtml teplate, I just call a static block with the name of the category. Unfortunately, I can't get the XML to work :/

This is what I added to MyTheme/Magento_Catalog/layout/catalog_category_view.xml

 <block class="Magento\Catalog\Block\Category\View" name="category.bottom" template="Magento_Catalog::category/bottom.phtml"/> 

And in the template directory under category, I added the bottom.phtml. But when I open a category now, it says "File path is forbidden for security reasons".

So I tried to just call a template that is already there (the description.phtml), but that did not work either.

Now it gets strange: I tried to override the description.phtml in my theme, just to test if I got the paths right. The result was that the description in the frontend just disappeard, no matter what I wrote in the file - even if I just copied the original file to the override file. I just can't understand why...

Can anyone help?

Thanks, Valentin

1 Answer 1

2

You have to create a small extension for this requirement.

First, create a block class View.php which generates for bottom cms section which should be located at app/code/Parkourstore/Catalog/Block/Category

<?php /** * Created by Amit Bera. * User: Amit Kumar Bera * Email: [email protected] * Date: 13-06-2018 * Time: 09:04 */ namespace Parkourstore\Catalog\Block\Category; class View extends \Magento\Catalog\Block\Category\View { public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Catalog\Model\Layer\Resolver $layerResolver, \Magento\Framework\Registry $registry, \Magento\Catalog\Helper\Category $categoryHelper, array $data = []) { parent::__construct($context, $layerResolver, $registry, $categoryHelper, $data); } /** * @return mixed */ public function getCmsBlockBottomHtml() { if (!$this->getData('cms_block_bottom_html')) { $html = $this->getLayout()->createBlock( \Magento\Cms\Block\Block::class )->setBlockId( $this->getCurrentCategory()->getName() )->toHtml(); $this->setData('cms_block_bottom_html', $html); } return $this->getData('cms_block_bottom_html'); } } 
  • Second, you have create a template file for this block at app/code/Parkourstore/Catalog/view/frontend/templates/category/bottom-cms.phtml
 <?= $block->getCmsBlockBottomHtml() ?> 
  • 3rd you have to call this block using layout so create `catalog_category_view.xml

`

<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceContainer name="content.bottom"> <block class="{VendorName}\{ModuleName}\Block\Category\View" name="category.cms.bottom" template="{VendorName}_{ModuleName}::category/bottom-cms.phtml"/> </referenceContainer> </body> </page> 
5
  • Thank you! Unfortunately, I get an two exceptions: ` Exception #0 (Magento\Framework\Exception\LocalizedException): Invalid block type: Parkourstore\Catalog\Block\Category\View Exception #1 (ReflectionException): Class Parkourstore\Catalog\Block\Category\View does not exist`. Here is a screenshot how I build the module, maybe wrong paths? prntscr.com/junklq Commented Jun 13, 2018 at 21:07
  • And I added the XML in my theme (Magento_Catalog/layout/catalog_category_view.xml) Commented Jun 13, 2018 at 22:32
  • Location seems ok from screesnhot. But might be block class name is wrong, Please check updated post. Commented Jun 14, 2018 at 7:18
  • Stupid me forgot to copy the opening php tag >< Thank you so much! Works now! I changed it to 'setBlockId( "cat-bottom-" . $this->getCurrentCategory()->getId() )->toHtml();' and it works perfectly! Commented Jun 14, 2018 at 12:21
  • How to add cms block using custom module instead of adding cms block from magento admin backend magento.stackexchange.com/q/330082/57334 Commented Jan 27, 2021 at 9:13

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.