1

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 } ?>
  • Above code is working with objectmanager or not? – Rakesh Jesadiya Feb 8 '17 at 11:46
  • Hi Rakesh, above code does work - but does not work if flat catalog is enabled. – YorkieMagento Feb 8 '17 at 12:35
  • U got ur answer @YorkieMagento? – Ankit Shah Feb 9 '17 at 3:20
  • Have you faced any error after enable flat category and flat catalog? – Rakesh Jesadiya Feb 10 '17 at 8:50
2
+50

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 } ?>
0

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

$categories = $this->_catalogLayer->getCurrentCategory()->getChildrenCategories();
  • Thanks John, unsure where I'd apply that code in the above... not sure if you can help further please? – YorkieMagento Feb 4 '17 at 10:37
  • Inside which block is your code located? – Jonathan Ribas Feb 4 '17 at 10:43
  • Hi john, the code is in 'categorylist.phtml' and that file is called in a static block. – YorkieMagento Feb 4 '17 at 10:44
  • Any further assistance you can give John - about where to place your code in the above? Thank you. – YorkieMagento Feb 6 '17 at 15:48
  • Change your block class by {{block class="Magento\Catalog\Block\Category\View" template="Magento_Theme::html/categoryblockssteve.phtml"}} and inside categoryblockssteve.phtml put this code: $subcategories = $block->getCurrentCategory()->getChildrenCategories(); var_dump($subcategories); – Jonathan Ribas Feb 7 '17 at 21:35

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.