Question

I have the following code in a phtml fill that is called in a static block and produces a list of sub-categories for the parent. It currently does not work when flat catalog is enabled. Can anyone suggest an update to get this code to work with flat catalog enabled?

CURRENT CODE IN CMS>STATIC BLOCK

{{block class="Magento\Framework\View\Element\Template" template="Magento_Theme::html/categoryblockssteve.phtml"}}

CURRENT CODE IN TEMPLATE/PHTML

<?php  


$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$request = $objectManager->get('Magento\Framework\App\Action\Context')->getRequest();
//echo $request->getFullActionName();
if($request->getFullActionName() === "catalog_category_view"){ ?>

    <?php    
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');
    /*print_r($category->getData());
    exit();*/
    ?>

    <?php if($category->getDescription()) { ?>
             <?php echo $category->getDescription(); ?>
             <?php /*
                <h1><?php echo $category->getName(); ?></h1>
                <p><?php echo $category->getDescription(); ?></p>
                 */ ?>
    <?php } ?>

    <div class="categor_list row">
        <ul>
            <?php

            //print_r($category->getData());
            //echo $category->getEntityId();


            $_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $object_manager = $_objectManager->create('Magento\Catalog\Model\Category')->load($category->getEntityId());
            $testse = $object_manager->getChildrenCategories()->getData();

            //exit();
            /*$sublist = $category->getChildren();
            $testse  = explode(",",$sublist);*/
            /*print_r($testse);
            exit();*/
            foreach ($testse as $subids) {

                $_objectManagers = \Magento\Framework\App\ObjectManager::getInstance();

                $object_managers = $_objectManagers->create('Magento\Catalog\Model\Category')->load($subids['entity_id']); ?>

                <li class="col-md-3">

                    <div class = "cate_name">
                        <a href="<?php echo $object_managers->getUrl(); // URL  ?>" alt="<?php echo $object_managers->getName(); // Name ?>">
                            <?php echo $object_managers->getName(); // Name ?>
                        </a>
                    </div>
                    <div class = "cate_img">
                        <a href="<?php echo $object_managers->getUrl(); // URL  ?>">

                            <?php if($object_managers->getImageUrl()) { ?>
                                <div class ="img"> <img src="<?php echo $object_managers->getImageUrl(); //Img Path ?>" alt="<?php echo $object_managers->getName(); // Name ?>"></div >
                            <?php }/* else{ ?>
                                <div class ="img"> <img src="<?php echo ""; //Thamnel img ?>"></div >
                                    <?php }  */?>

                        </a>
                    </div>
                </li>

            <?php     } ?>
        </ul>
    </div>
<?php echo $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('all_cat_bottom')->toHtml();?>
<?php } ?>
Was it helpful?

Solution

To get child categories, you can use getChildrenCategories() which will also work with Flat settings.

But also note that, if flat is enabled, then you don't need to bring disabled category in frontend

Please modify your phtml file like this


<?php  


$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$request = $objectManager->get('Magento\Framework\App\Action\Context')->getRequest();
//echo $request->getFullActionName();
if($request->getFullActionName() === "catalog_category_view"){ ?>

<?php    
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');
    echo $category->getName();
    $SubCategories = $category->getChildrenCategories();


?>

 <div class="categor_list row">
        <ul>
            <?php
            if($SubCategories){
            foreach ($SubCategories as $SubCategory) 
            {

                $_objectManagers = \Magento\Framework\App\ObjectManager::getInstance();

                $SubCategoryOb = $_objectManagers->create('Magento\Catalog\Model\Category')->load($SubCategory->getId()); ?>

                <li class="col-md-3">

                    <div class = "cate_name">
                        <a href="<?php echo $SubCategoryOb->getUrl(); // URL  ?>" alt="<?php echo $SubCategoryOb->getName(); // Name ?>">
                            <?php echo $SubCategoryOb->getName(); // Name ?>
                        </a>
                    </div>
                    <div class = "cate_img">
                        <a href="<?php echo $SubCategoryOb->getUrl(); // URL  ?>">

                            <?php if($SubCategoryOb->getImageUrl()) { ?>
                                <div class ="img"> <img src="<?php echo $SubCategoryOb->getImageUrl(); //Img Path ?>" alt="<?php echo $SubCategoryOb->getName(); // Name ?>"></div >
                            <?php } ?>

                        </a>
                    </div>
                </li>

            <?php     } ?>
            <?php  }  // end of f($SubCategories) ?>
        </ul>
<?php echo $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('all_cat_bottom')->toHtml();?>
<?php } ?>

OTHER TIPS

Avoid the use of ObjectManager, try using this code vendor/magento/module-catalog/Block/Navigation.php at line 187

$categories = $this->_catalogLayer->getCurrentCategory()->getChildrenCategories();
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top