Magento 2 – Get Category Count with getChildCategories()

categorymagento2menu

I am want to create the category menu on home page and my HTML structure in the UL LI. So I have 3rd level of categories like the below:

Parent > Child > sub Child.

I want to count the sub child categories for list on 5 sub categories in the menu. I am using the below code to get the categories:

<?php
  $categoryHelper = $this->helper('Magento\Catalog\Helper\Category');
  $maincount++;
  foreach($categoryHelper->getStoreCategories() as $category): 
?>
 <li><a href="<?php echo $categoryHelper->getCategoryUrl($category) ?>"><?php echo $category->getName() ?></a></li>
<?php endforeach; ?><li><a href="<?php echo $categoryHelper->getCategoryUrl($category) ?>"><?php echo $category->getName() ?></a>
                        <?php  
                            if($childrenCategories = $this->getChildCategories($category)):
                        ?>
                            <div class="sub-category-section">
                                <div class="sub-category">
                                    <div class="sub-category-inner">
                                        <ul class="level1">
                                        <?php $subcount = 1;?>
                                        <?php foreach($childrenCategories as $categoryChild) :
                                              $subChild = $this->getChildCategories($categoryChild);
                                        ?>
                                            <li><a href="<?php echo $categoryHelper->getCategoryUrl($category) ?>"><?php echo $category->getName() ?></a>
                                            <?php if($subChild = $this->getChildCategories($categoryChild)):?>
                                                <ul class="level2">
                                                    <?php $subsubcount=1; ?>
                                                    <?php foreach($subChild as $_categoryGrandchild) : ?>
                                                        <li>
                                                            <a href="<?php echo $categoryHelper->getCategoryUrl($_categoryGrandchild) ?>"><?php echo $_categoryGrandchild->getName() ?></a>
                                                        </li>
                                                        <?php 
                                                        $subsubcount++;
                                                        if($subsubcount > 5){break;}

                                                            ?>
                                                    <?php endforeach; ?>

                                                        <?php 
                                                        if($subChild->count() > 5){?>
                                                        <li><a href="<?php echo $_categoryChildModel->getUrl()?>" class="view-more"> <?php $this->__('View All'); ?></a> </li>
                                                        <?php } ?>
                                                </ul>
                                            <?php endif; ?>
                                            <?php if($subcount%2==0){echo "</li></ul></div><div class='sub-category-inner'><ul class='level1'>"; }?>
                                        <?php $subcount++; if($subcount > 3){break;}?>
                                        <?php endforeach;?>
                        <?php endif; ?>
                    </li>
                <?php $maincount++; endforeach; ?>

Also, want to get the products count for each category. When I use the $subcount > 3 I am getting the fatal error.

Fatal error: Call to a member function count() on array in E:\xampp\htdocs\idealmall\app\code\Ibnab\CategoriesSide\view‌​\frontend\templates\‌​storecategories.phtm‌​l on line 48

Best Answer

The error seems to be on your code,

<?php if($subChild->count() > 5){ ?>
    <li><a href="<?php echo $_categoryChildModel->getUrl()?>" class="view-more"> <?php $this->__('View All'); ?></a> </li>
<?php } ?>

It seems that your $subChild object does not have count() method. Replace it with below,

<?php if(count($subChild) > 5){ ?>
    <li><a href="<?php echo $_categoryChildModel->getUrl()?>" class="view-more"> <?php $this->__('View All'); ?></a> </li>
<?php } ?>