Question

I am trying to get only parent categories of a category id 4 but right now instead of parent category it is listing all child categories. Below is the code that I currently have in .phtml template file. Is there something wrong in the code? How could I achieve it?

<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = Mage::getModel('catalog/category')->getCategories(4); ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
    <?php foreach($_categories as $_category): ?>
        <?php
            //arrange by Letter
            foreach($_category->getChildren() as $_sub){
                $letter = substr(strtolower($_sub->getName()),0,1);
                $subCategories[$letter][0]['name'] = $letter; //0 index is the letter
                $i = 1;
                while(isset($subCategories[$letter][$i])){
                    $i++;
                }
                $subCategories[$letter][$i]['name'] = $_sub->getName();
                $subCategories[$letter][$i]['url'] = $_helper->getCategoryUrl($_sub);
            }

        ?>
    <?php endforeach ?>
    <?php
        $i = 0;
        $x = 0;
        $d = 0;
        $splits = 1;
        foreach($subCategories as $sub){
            $d += count($sub); //split it into 3rds
        }
        $d = round($d / 5);
        //get a count of all elements for splitting
        ksort($subCategories);
        foreach($subCategories as $sub){
            $i = 0;
            //$d = round(count($sub) / 3); //split it into 3rds

            echo '<div class="catContainer">';
            asort($sub);
            $displayHead = TRUE;
            while(isset($sub[$i])){
                if($x % $d == 0 && $x != 0 && $splits != 5){
                    $splits++;
                    echo '</div></div><div class="containerContainer"><div class="catContainer">';
                    if($i != 0){
                        echo '<div class="catContainerTop">'.$sub[0]['name'].'</div>';
                    }
                }
                if($i == 0){
                    echo '<div class="catContainerTop">'.$sub[0]['name'].'</div>';
                } else if($i != 0){
                    echo '<span><a href="'.$sub[$i]['url'].'">' . $sub[$i]['name'] . '</a></span><br />';
                }
                $i++;
                $x++;
            }
            echo '</div>';
        }
    ?>
<?php endif; ?>

Update: I did try adding <?php $subCategories = array(); ?> before <?php if (count($_categories) > 0): ?> as suggested but it is not helping at all so anyone with the suggestion and solutions?

Was it helpful?

Solution

Your code actually does not make sense to me. You say you want to load category with id 4, but the code does not reflect that.

To load category with ID 4, you'd use $currentCategory = mage::getModel('catalog/category')->load(4)

which would then give you one category object.

Then to get its parent categories, you use the getPath() call on that category object. This will result in a string in format 'id/id/id/id' which is the path of categories of category with id 4, up to your base category.

Thus, to get the parent categories of category id 4, you'd explode that to an array, which then will give you an array of parent categories (up the tree) to work with.

Ideally you'd not want to do a loop and load each of them, but do one collection to load them all as a collection.

Just as a hint. You are placing too much logic into your phtml. You should create a block class and move the business logic into the block class. In the long run that will be much more maintainable.

Good luck

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top