开发者

Display all children categories and their children in magento

So I want this code to display the categories children and their children on my view.phtml page in app/frontend/default/default/catalog/category/

So when you see the category page you see ALL of the children from all the sub categories

Here is what I got, it is showing sub categories, but not their children.

    <?php
    $_category  = $this->getCurrentCategory(); 
    $collection = Mage::getModel('catalog/category')->getCategories($_category-       >entity_id);
    $helper     = Mage::helper('catalog/category');
    ?>

   <ul>
    <?foreach ($collection as $cat):?>
            <?php if($_category->getIsActive()):?>
            <?php 
                 $cur_category = Mage::getModel('catalog/category')->load($cat->getId());
                 $_img = $cur_category->getImageUrl();  
            ?>
            <li>
                            <a href="<?php echo $helper->getCategoryUrl($cat);?>">
                                 <img src="<?php echo $_img?>" title="$cat->getNam开发者_如何学JAVAe()"/>
                                 <cite><?php echo $cat->getName();?></cite>
                            </a>
                    </li>
            <?php endif?>

<?php endforeach;?>


Please try to use following code. Main idea is get main level category. For each category get children categories.

<?php
      /* Get the categories that are active for the store */
      $_main_categories=$this->getStoreCategories();

      /* Get the current category the user is in */
      $_current_category=$this->getCurrentCategory();

      /* Get the current category path */
      $_categorypath = $this->getCurrentCategoryPath();
?>

    <?php if ($_main_categories): ?>
        <div class="box normal-nav">
            <div class="box-top">
            </div>
            <div class="box-content">
                    <ul>
                        <?php
                            /* This bit cycles through the categories - setting the next one to current */
                            foreach ($_main_categories as $_main_category):
                                if($_main_category->getIsActive()):
                                    $cur_category=Mage::getModel('catalog/category')->load($_main_category->getId());
                                    $layer = Mage::getSingleton('catalog/layer');
                                    $layer->setCurrentCategory($cur_category);
                        ?>

                                    <li><a href="<?php echo $this->getCurrentCategory()->getUrl()?>"><?php echo $this->getCurrentCategory()->getName();?></a>

                                        <?php $_maincategorylisting=$this->getCurrentCategory()?>

                                        <?php $_categories=$this->getCurrentChildCategories()?>

                                        <?php if($_categories->count()): ?>
                                            <ul class="subcategory">
                                                <? foreach ($_categories as $_category):?>
                                                   <? if($_category->getIsActive()):
                                                          $cur_subcategory=Mage::getModel('catalog/category')->load($_category->getId());
                                                          $layer = Mage::getSingleton('catalog/layer');
                                                          $layer->setCurrentCategory($cur_subcategory);
                                                   ?>

                                                          <li><a href="<?php echo $this->getCategoryUrl($_category)?>"> <?php echo $_category->getName()?></a></li>
                                                   <? endif;?>

                                                 <?endforeach?>

                                            </ul>
                                            <?php $layer->setCurrentCategory($_current_category);  ?>

                                        <? endif; ?>
                                    </li>

                             <?php endif; ?>

                        <?php endforeach; ?>
                    </ul>
                </div>
                <div class="box-bottom">

                </div>
        </div>
    <?php endif;  ?>

Added later:

If apply fix for your code is look like following:

    <?php
        $_category  = $this->getCurrentCategory();
        $collection = Mage::getModel('catalog/category')->getCategories($_category->entity_id);
        $helper     = Mage::helper('catalog/category');
    ?>
   <ul>
    <?php foreach ($collection as $cat):?>
            <li>
                <a href="<?php echo $helper->getCategoryUrl($cat);?>">
                    <cite><?php echo $cat->getName();?></cite>
                </a>
                <?php $childLevel2Category = Mage::getModel('catalog/category')->getCategories($cat->entity_id); ?>
                <ul>
                    <?php foreach ($childLevel2Category as $catLevel2) { ?>
                        <li>
                            <a href="<?php echo $helper->getCategoryUrl($catLevel2);?>">
                                <cite><?php echo $catLevel2->getName();?></cite>
                            </a>
                        </li>
                    <?php } ?>
                </ul>
            </li>
<?php endforeach;?>
   </ul>

If you need more level (more sub-dir) rewrite this construction using recursive function.


Try changing the "Is Anchor" setting for these parent categories to "Yes". That should allow them to show the products of their child categories without any custom coding.


I know this is an old question, but I came across it in a search while trying to do the same thing.

For the benefit of future visitors, one of the answers above talks about changing Is Anchor for the parent categories to Yes, in order to show the children. The OP said it didn't work for him.

Crucially, after doing this, you need to reindex your category/products associations (System->Index Management). This may take some time if you have a lot of products.

But after doing so... voila! Descendant products are listed.

And placing this code from http://www.creare.co.uk/magento-subcategories-category-pages in template/catalog/category/list.phtml took care of displaying the sub cats:

<?php
$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
$categories = $category->getCollection()
        ->addAttributeToSelect(array('name', 'thumbnail'))
        ->addAttributeToFilter('is_active', 1)
        ->addIdFilter($category->getChildren())
?>
<ul class="subcategories">
    <?php foreach ($categories as $category): ?>
        <li>
            <a href="<?php echo $category->getUrl() ?>"><img src="<?php echo Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . $category->getThumbnail() ?>" alt="<?php echo $this->htmlEscape($category->getName()) ?>" />
                <span><?php echo $category->getName() ?></span></a>
        </li>
    <?php endforeach; ?>
</ul>

Hope this helps someone.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜