Anyone can help me to show show static block based on current category id

In this following file


I tried this code to get current id

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category
echo $category->getId();
echo $category->getName();

Its returns with this error Fatal error: Uncaught Error: Call to a member function getId()

Magento 2.1.6

  • Why are you using that template? Are you using that phtml (Magento_Swatches/templates/product/view/renderer.phtml) because it is the location that you are looking for the static block to show up? Or do you need some of the swatch logic to in that call to the static block?
    – circlesix
    Commented Jun 16, 2017 at 21:57
  • Yes i need to show size chart .
    – MagEGY
    Commented Jun 16, 2017 at 22:19
  • follow this link mage2.pro/t/topic/5486
    – sudo55
    Commented Jun 26, 2020 at 11:10

1 Answer 1


While it might be tempting to put this is a template override, it's really better to make a module to get this kind of functionality. Using the object manager is a bad idea here, as it's hard to know how long Magento will keep it around, and i think you are running into issues with the scope of the category id on the product page that is making it so it's not working the way you would expect.

You could do something like this:


namespace Vendor\SizeChart\Block;

use Magento\Framework\View\Element\Template;
use Magento\Backend\Block\Template\Context;
use Magento\Framework\Registry;

class SizeChart extends Template
    protected $_registry;

    public function __construct(
        Context $context,
        Registry $registry,
        array $data = []
        $this->_registry = $registry;
        parent::__construct($context, $data);

    public function getCurrentCategory()
        return $this->_registry->registry('current_category');

This is create a block that you can get the Magento registry from, extend from the Template class and return a method that had the current category object, all while avoiding the object manager.


<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <referenceBlock name="product.info.options.wrapper">
            <block class="Vendor\SizeChart\Block\SizeChart" name="custom-size-chart" after="-" template="Vendor_SizeChart::size-chart.phtml"/>

From that block class, you can get a custom template and place it where you need it on the product page using just xml, no core template overrides needed. You might have to play with the placement by finding the right block to reference, here i'm using product.info.options.wrapper as it seems close to where you wanted it.


<?php $currentCategory = $block->getCurrentCategory()->getId(); ?>
<?php if ($currentCategory = '26') : ?>
    <div class="size-chart">
        <?php echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('size-chart')->toHtml();?>
<?php endif; ?>

Of course you will have to set the category value based on what you are trying to achieve. I'm using '26' here just for my testing, you might find different needs, just check in the admin what category numbers you need. From there i just call in a cms block named size-chart, where you can put any content you need.

You just have to register the module and run setup:upgrade in the cli.


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_SizeChart" setup_version="1.0.0" />



Just a note, I took the block idea from here: How to Get Product Current Category Name in Magento 2 in Product View Page and adapted it to what you are trying to do with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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