Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
366 views
in Technique[技术] by (71.8m points)

Magento: Can't get categories children

For some reason this code is returning NULL when trying to get a categories subcategories.

 <?php var_dump($_category->getChildrenCategories()); ?>

Here is the full code from a .phmtl file.

<ul id="nav_vert">
            <?php foreach ($this->getStoreCategories() as $_category): ?>
            <?php if ($_category->getIsActive()) { ?>
            <?php $open = $this->isCategoryActive($_category); ?>
            <?php $potential = $_category->hasChildren(); ?>
            <li><a href="<?php echo $this->getCategoryUrl($_category); ?>"<?php if($open) { echo ' class="open"'; } ?><?php if($potential) { echo ' class="potential"'; } ?> ><?php if($potential&&$open) { echo 'v '; } elseif($potential) { echo '> '; }else{ echo '  '; }?><?php echo $_category->getName();?></a>
                <?php if ($open && $potential): ?>
                <?php var_dump($_category->getChildrenCategories()); ?>
                <ul>
                    <?php foreach ($_category->getChildrenCategories() as $subcategory): ?>
                    <?php $subCat = Mage::getModel('catalog/category')->load($subcategory); ?>
                    <?php $open = $this->isCategoryActive($subCat); ?>
                    <?php $potential = $subCat->hasChildren(); ?>
                    <li><a href="<?php echo $this->getCategoryUrl($subCat); ?>"<?php if($open) { echo ' class="subopen"'; } ?><?php if($potential) { echo ' class="potential"'; } ?><?php if(!$potential&&$open) { echo ' class="final"'; } ?> ><?php if($potential&&$open) { echo ':: '; } elseif($potential) { echo '> '; }?><?php echo $subCat->getName(); ?> (<?php echo $subCat->getProductCount(); ?>)</a>
                        <?php if ($open && $potential): ?>
                        <ul>
                            <?php foreach ($subcategory->getChildrenCategories() as $subsubcategory): ?>
                            <?php $subsubCat = Mage::getModel('catalog/category')->load($subsubcategory); ?>
                            <?php $open = $this->isCategoryActive($subsubCat) ?>
                            <li><a href="<?php echo $this->getCategoryUrl($subsubCat); ?>" <?php if($open) { echo ' class="final"'; } ?>><?php echo $subsubCat->getName(); ?> (<?php echo $subsubCat->getProductCount(); ?>)</a></li>
                            <?php endforeach; ?>
                        </ul>
                        <?php endif; ?>
                    </li>
                    <?php endforeach; ?>
                </ul>
                <?php endif; ?>
            </li>
            <?php } ?>
            <?php endforeach ?>
        </ul>

This line always returns true

<?php $potential = $_category->hasChildren(); ?>

And I know that the category has children.

Can anyone suggest why this doesn't work?

This is how I place the phtml in the page:

<reference name="left">
    <block type="catalog/navigation" name="catalog.vertnav" template="catalog/navigation/vert_nav.phtml" before="-" />
</reference>

Magento version 1.5.1.0

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try using $_category->getChildren() (instead of $_category->getChildrenCategories() )


Have an easy day,
Pesach


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...