2

I'm tryng to display categories and subcategories on a cms page

<?php 
    $categories = Mage::getModel('catalog/category')
        ->getCollection()
        ->addAttributeToSelect('*')
        ->addIsActiveFilter();
    foreach ($_categories->getStoreCategories() as     $_category) { 
        $category = Mage::getModel('catalog/category'); 
        $category->load($_category->getId()); 
        $subcategories = explode(',', $category->getChildren()); 
?> 
        <dl> 
          <dt><?php echo $this->htmlEscape($_category->getName()); ?>
          </dt> 
          <dd> 
            <ol> 
              <?php 
                foreach ($subcategories as $subcategoryId) { 
                  $category->load($subcategoryId); 
                  echo '<li><a href="' . $category->getURL() . '">' . $category->getName() . '</a></li>'; 
                } 
              ?> 
            </ol> 
          </dd> 
        </dl> 
    <?php
      } 
    ?>

But this is returning me a fatal error Fatal error: Call to a member function getStoreCategories() on null on line 5

  • change $_categories->getStoreCategories() to $categories->getStoreCategories() i thing $_categories variabale is not define there – Vaibhav Ahalpara Oct 26 '16 at 11:52
0

You can try below code for display all store category and subcategories.

<?php
    $_helper = Mage::helper('catalog/category');
    $categories = $_helper->getStoreCategories();

    foreach ($categories as $_category) {
        $category = Mage::getModel('catalog/category')->load($_category->getId());
        $subcategories = $category->getChildrenCategories();
        ?>
        <dl>
            <dt><?php echo $this->htmlEscape($_category->getName()); ?></dt>
            <dd>
                <ol>
                    <?php
                    foreach ($subcategories as $_subcategory) {
                        echo '<li><a href="' . $_subcategory->getURL() . '">' . $_subcategory->getName() . '</a></li>';
                    }
                    ?>
                </ol>
            </dd>
        </dl>
        <?php

    }
    ?>

Let me know if you have any query from above.

  • This will only work in one level, if subcategories have their own subcategories then? – Haris Nov 27 '17 at 19:15
0

Please follow the below steps:

Step 1: Create new_list.phtml in frontend/your_theme/default/template/catalog/category and paste the below code :

     ================code starts here================

<div class="block block-list block-categories">
    <div id="block-categories" class="block-title active">
        <strong><span>Categories </span></strong>
    </div>  
    <div id="leftnav" class="block-content" style="display:block">
        <?php $helper = $this->helper('catalog/category') ?>
            <?php $categories = $this->getStoreCategories() ?>
        <?php if (count($categories) > 0): ?>
            <ul id="leftnav-tree" class="level0">
                <?php foreach($categories as $category): ?>
                    <li class="level0<?php if ($this->isCategoryActive($category)): ?> active<?php endif; ?>">
                        <a href="<?php echo $helper->getCategoryUrl($category) ?>"><span><?php echo $this->escapeHtml($category->getName()) ?></span></a>
                        <?php //if ($this->isCategoryActive($category)): ?>
                            <?php $subcategories = $category->getChildren() ?>
                            <?php if (count($subcategories) > 0): ?>
                                <ul id="leftnav-tree-<?php echo $category->getId() ?>" class="level1">
                                    <?php foreach($subcategories as $subcategory): ?>
                                        <li class="level1<?php if ($this->isCategoryActive($subcategory)): ?> active<?php endif; ?>">
                                            <a href="<?php echo $helper->getCategoryUrl($subcategory) ?>"><?php echo $this->escapeHtml(trim($subcategory->getName(), '- ')) ?></a>
                                             <?php $secondLevelSubcategories = $subcategory->getChildren() ?>
                                             <?php if (count($secondLevelSubcategories ) > 0): ?>
                                <ul id="leftnav-tree-<?php echo $subcategory->getId() ?>" class="level2">
                                    <?php foreach($secondLevelSubcategories as $secondLevelSubcategory ): ?>
                                        <li class="level2<?php if ($this->isCategoryActive($secondLevelSubcategory )): ?> active<?php endif; ?>">
                                            <a href="<?php echo $helper->getCategoryUrl($secondLevelSubcategory ) ?>"><?php echo $this->escapeHtml(trim($secondLevelSubcategory ->getName(), '- ')) ?></a>
                                        </li>
                                        <?php endforeach; ?>
                                </ul>
                                <script type="text/javascript">decorateList('leftnav-tree-<?php echo $category->getId() ?>', 'recursive')</script>
                            <?php endif; ?>
                                    <?php endforeach; ?>
                                </ul>
                                <script type="text/javascript">decorateList('leftnav-tree-<?php echo $category->getId() ?>', 'recursive')</script>
                            <?php endif; ?>
                        <?php //endif; ?>
                    </li>
                <?php endforeach; ?>
            </ul>
            <script type="text/javascript">decorateList('leftnav-tree', 'recursive')</script>
        <?php endif; ?>
    </div>
</div>

          ===============code ends here===============

Step 2 : Call new_list.phtml in your CMS page or static block:

{{block type="catalog/navigation" name="catalog.category" template="catalog/category/new_list.phtml"}}

Hope, it will help.

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.