0

OK I have taken over a store from a group of developers. In sub-cat.phtml they are ordering the sub-categories by their ID. I would like to change this to be either the order they're in within admin area or from A-Z. This is the code from the template file:

<div class="sub-category">
<?php
    $category = Mage::getModel('catalog/category')->load($subCategoryId);
    $object = new Mage_Catalog_Block_Navigation();
    $actualCategoryId = $object->getCurrentCategory()->getId();
    $actualCategory = Mage::getModel('catalog/category')->load($actualCategoryId);
    $subCategories = explode(',', $actualCategory->getChildren());
    $count_sub_cat = count($subCategories);
    $count_sub_cat_half = $count_sub_cat/3;
?>

<ul>

<?php
    $count=1;
    $_iterator = 0;
    foreach ( $subCategories as $subCategoryId ) {      
?>

<li class="sub-cat <?php if ($count%4 == 0): ?> last<?php endif; ?>">       
    <div class="sub_cat_inner">

        <?php

            $category = Mage::getModel('catalog/category')->load($subCategoryId);

            if( $category->getIsActive() ) {

                $_helper    = $this->helper('catalog/output');
                $_category  = $this->getCurrentCategory();
                $_imgHtml   = '';
                $thumb = $category->getThumbnail();
                echo '<p class="category-image-list"><a href="'.$category->getURL().'">';
                echo '<img src="'.$this->getBaseUrl().'media/catalog/category/'.$thumb.'" alt="'.$this->htmlEscape($_category->getName()).'" title="'.$this->htmlEscape($category->getName()).'" style="max-width:215px" />';
                echo '</a></p>';
                echo '<a class="cat-name" href="'.$category->getURL().'"><span>'.$category->getName().'</span></a> ';
            }

        ?>

    </div>  
</li>

<?php if($count == $count_sub_cat) echo '</ul>';
    else if ($count%4 == 0) { 
        echo '</ul><ul>';
    }
?>

<?php  $count++; } ?>
</div>

I'm wondering if Magento provides an easy method for doing this?

  • @FabianBlechschmidt: My question is different. – Adrift Jul 17 '14 at 22:14
  • 1
    This is really not good code in a Magento template. – benmarks Jul 18 '14 at 1:41
  • @FabianBlechschmidt, benmarks: Can you explain one thing to me? How is the above code generating the sub-sub categories? When I debug, it only seems to print out sub-categories of the root cateogry. It's making me very frustrated. Also why is this not good code? – Adrift Jul 18 '14 at 14:46
  • I would say you are right. This code only creates entries for all categories under the current one and one entry for $subCategoryId – Fabian Blechschmidt Jul 18 '14 at 14:50
1

I have change the code check

    <div class="sub-category">
<?php
    $category = Mage::getModel('catalog/category')->load($subCategoryId);
    $object = new Mage_Catalog_Block_Navigation();
    $actualCategoryId = $object->getCurrentCategory()->getId();
    $actualCategory = Mage::getModel('catalog/category')->load($actualCategoryId);
    $subCategories = explode(',', $actualCategory->getChildren());
    $count_sub_cat = count($subCategories);
    $count_sub_cat_half = $count_sub_cat/3;
?>

<ul>
<?php
    $count=1;
    $_iterator = 0;
    $CatColection= Mage::getModel('catalog/category')->getCollection()
            ->addNameToResult()
            ->addUrlRewriteToResult()
            ->addIsActiveFilter()
        ->addAttributeToFilter('entity_id',array("in"=>$subCategories))
            ->setOrder('name',ASC);
    foreach ($CatColection as $subCategoryId ) {      
?>

<li class="sub-cat <?php if ($count%4 == 0): ?> last<?php endif; ?>">       
    <div class="sub_cat_inner">

        <?php
            $subCategoryId=$subCategory->getId();
            $category = Mage::getModel('catalog/category')->load($subCategoryId);

            if( $category->getIsActive() ) {

                $_helper    = $this->helper('catalog/output');
                $_category  = $this->getCurrentCategory();
                $_imgHtml   = '';
                $thumb = $category->getThumbnail();
                echo '<p class="category-image-list"><a href="'.$category->getURL().'">';
                echo '<img src="'.$this->getBaseUrl().'media/catalog/category/'.$thumb.'" alt="'.$this->htmlEscape($_category->getName()).'" title="'.$this->htmlEscape($category->getName()).'" style="max-width:215px" />';
                echo '</a></p>';
                echo '<a class="cat-name" href="'.$category->getURL().'"><span>'.$category->getName().'</span></a> ';
            }

        ?>

    </div>  
</li>

<?php if($count == $count_sub_cat) echo '</ul>';
    else if ($count%4 == 0) { 
        echo '</ul><ul>';
    }
?>

<?php  $count++; } ?>
</div>
| improve this answer | |
  • It's not working for me. The sub-categories don't show up if I use your code. – Adrift Jul 17 '14 at 19:15
  • I'm getting the same result after edit :( – Adrift Jul 18 '14 at 12:00
  • Check now again............. – Amit Bera Jul 18 '14 at 15:50

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.