0

I'm using this code to show subcategories on the category page. I was working fine in 1.9.2.0 but is broken in 1.9.2.2. Does anyone have the updated code?

<?php 
$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
$categories = $category->getCollection()
        ->addAttributeToSelect(array('name', 'image'))
        ->addAttributeToFilter('is_active', 1)
        ->addIdFilter($category->getChildren())
?>
<h2>Choose A Subcategory...</h2>
<ul class="subcategories">
    <?php foreach ($categories as $category): ?>
        <li>
            <a href="<?php echo $category->getUrl() ?>"><img src="<?php echo $category->getImageUrl() ?>" alt="<?php echo $this->htmlEscape($category->getName()) ?>" /><br />
                <span><?php echo $category->getName() ?></span></a>
        </li>
    <?php endforeach; ?>
</ul>
<span class="clear"></span>
2

Thanks for the help, but it turned out to be related to a bug in Magento 1.9.2.2. http://www.magentocommerce.com/bug-tracking/issue/index/id/870

  • @brentwpeterson Are you running a "no thanks" bot?? – benmarks Nov 14 '15 at 2:17
  • @benmarks SE adds that text automagically. Should I be doing something different when reviewing posts? – brentwpeterson Nov 14 '15 at 13:17
  • @brentwpeterson Just wondered bc that wasn't a "thank you" answer. – benmarks Nov 15 '15 at 7:34
1

1.9.0.1 working code, goes as deep as you permit it. Give it a try. I also put some example filters in there for others who read this post.

$collection = Mage::getModel('catalog/category')->getCollection()
             ->addAttributeToSelect('*')
             ->addAttributeToSelect('level')
             ->addAttributeToSelect('entity_id');


function recursion($catId) {
    $cat = Mage::getModel('catalog/category')->load($catId);

    if ($subcats = $cat->getChildren()) {
        foreach(explode(',',$subcats) as $subCatid) {
            $_category = Mage::getModel('catalog/category')->load($subCatid);
            // put your display code here
            // maybe also "recursion($subCatid);"
            // or "if($_category->getLevel() == 3)"
            // etc...
        }
    }
}

foreach($collection as $cat) {
    $catId = $cat->getId();
    recursion($catId);
}

Also try:

$currentCategory = Mage::registry('current_category');
  • Thanks - that's put me on a good path, but how do I restrict it to one level deep? – ljc Nov 3 '15 at 17:12
  • I altered the answer to go only one level deep. – Stuperfied Nov 3 '15 at 22:15
  • You may also wish to add a check for $_category->getIsActive() – Stuperfied Nov 3 '15 at 22:21

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.