i'm using below in detial-layout.phtml to show different blocks on product page based on the category ID:

<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); ?>
<?php   $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category'); ?>
<?php if($category->getId()==11): ?>
<?php echo $block->getChildHtml('men.shoes.static.block') ?>
<?php endif; ?>

But it is breaking the product page and displaying only the product image / gallery.

  • please check $category->getId() comming or not ? Jul 19 '18 at 10:25

The issue seems to be that you are trying to fetch current category on the product page which is breaking the page.
Instead of the current category, try to fetch product's category.

Also, don't use the object manager directly instead of it, load the dependencies in the constructor of your block.


By using the following module you can insert block on catalog page based on your category condition. This is only a sample, you need to modify it according to your requirement.




<?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="Anshu_Custom" setup_version="1.0.0">
            <module name="Magento_Catalog" />


<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <referenceContainer name="content">
            <block name="customblock" template="Anshu_Custom::customblock.phtml" before="-">
                    <argument name="viewModel" xsi:type="object">Anshu\Custom\ViewModel\Customblock</argument>


$viewModel = $block->getData('viewModel');
echo '<pre>';



namespace Anshu\Custom\ViewModel;

use Magento\Framework\View\Element\Block\ArgumentInterface;
use Magento\Catalog\Helper\Data;
use Magento\Framework\DataObject;

class Customblock extends DataObject implements ArgumentInterface
     * Catalog data.
     * @var Data
    private $catalogData;

     * @param Data $catalogData
    public function __construct(
        Data $catalogData
    ) {
        $this->catalogData = $catalogData;

    public function getSomeThing()
        // return array of assigned category Ids
        return $this->catalogData->getProduct()->getCategoryIds();

I have taken reference from breadcrumb implementation on the catalog page and the article https://firegento.com/blog/2017/12/07/better-blocks-magento-2-php-view-models/

  • could you please be more specific, tell how to do that? simple I just want: if any product in cat A display in product page bock 1 if any product in cat B display in product page bock 2 Jul 17 '18 at 11:11
  • Do you have any product that belongs to multiple categories? Jul 17 '18 at 11:19
  • No each product is belong to one cat Jul 17 '18 at 17:54
  • @AhmadDarwish you can check the updated answer and modify the module according to your requirement. Jul 18 '18 at 12:51

Try this if product belongs to only 1 category:

   <?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); ?>
    <?php   $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product'); 
 $categoryIds = $product->getCategoryIds();
$catId = current($categoryIds);
if($catId == 11): 
 echo $block->getChildHtml('men.shoes.static.block');  // or you can use this: echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('block_identifier')->toHtml();
 endif; ?>

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.