0

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
2
  • What error you are getting when you use $subcount > 3? Commented Feb 22, 2017 at 10:52
  • I am getting below error: Fatal error: Call to a member function count() on array in E:\xampp\htdocs\idealmall\app\code\Ibnab\CategoriesSide\view\frontend\templates\storecategories.phtml on line 48 Commented Feb 22, 2017 at 10:55

1 Answer 1

1

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 } ?>
1
  • Thanks Jaimin !!!!!. How I can get the Products count for each category. Commented Feb 22, 2017 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.