5

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.

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

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.

UPDATED

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.

app/code/Anshu/Custom/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Anshu_Custom',
    __DIR__
);

app/code/Anshu/Custom/etc/module.xml

<?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">
        <sequence>
            <module name="Magento_Catalog" />
        </sequence>
    </module>
</config>

app/code/Anshu/Custom/view/frontend/layout/catalog_product_view.xml

<?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">
    <body>
        <referenceContainer name="content">
            <block name="customblock" template="Anshu_Custom::customblock.phtml" before="-">
                <arguments>
                    <argument name="viewModel" xsi:type="object">Anshu\Custom\ViewModel\Customblock</argument>
                </arguments>
            </block>
        </referenceContainer>
    </body>
</page>

app/code/Anshu/Custom/view/frontend/templates/customblock.phtml

<?php
$viewModel = $block->getData('viewModel');
echo '<pre>';
print_r($viewModel->getSomeThing());

app/code/Anshu/Custom/ViewModel/Customblock.php

<?php 

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/

4
  • 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
3

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.