3

I have the following category structure in my magento store

- root
    - category 0 (ID 26)
        - category 1 (main)
            - category A (sub)
            - category V (sub)
        - category 2 (main)
            - category G (sub)
            - category J (sub)
            - category E (sub)
        - category 3 (main)
            - category L (sub)

and so on...

On a page i want to be able to output the following:

1. get each (main) category and its name and url
    2. get the first (sub) category in (main) and its icon (custom attribute)

So it would output this for each:

 <a href="main-cat-link"><img src="main-cat-first-sub-icon"></a>
 <a href="main-cat-link">main-cat-name</a>

Giving something like:

<category 1 link><category A icon img></link>
<category 1 link><category 1 name>

<category 2 link><category G icon img>
<category 2 link><category 2 link>

<category 3 link><category L icon img>
<category 3 link><category 3 link>

What is the best way of doing so? So far I have:

<?php
$_id = 26 // category 0
$_main_categories = Mage::getModel('catalog/category')
->getCollection()
->addFieldToFilter('parent_id', array('eq'=>$_id))
->addAttributeToFilter('is_active', 1)
->addAttributeToSelect(array('id','name','url'))

foreach($_main_categories as $_main_cat)
{
    // load full main category
    $_category = Mage::getModel('catalog/category')->load($_main_cat->getId());
    // get sub category's children
    $_main_cat_subs = $_category->getChildrenCategories();

    // loop through sub catagories, get icon in first and break
    foreach($_main_cat_subs as $_sub_cat) 
    {
        // load full sub category
        $_sub = Mage::getModel('catalog/category')->load($_sub_cat->getId());
        $i++;
            $_icon = $_sub_cat->getIcon();
        }
        if( $i >= 1 ){
            break; // as i only want the first one
        }
    }

    // output
    ?>
    <a href="<?php echo $_main_cat->getUrl() ?>">
        <img src="<?php echo $_icon ?>">
    </a>
    <a href="<?php echo $_main_cat->getUrl() ?>">
        <?php echo $_main_cat->getName() ?>
    </a>

<?php
}
?>

This works but seems very bad practice to load the full category models in a foreach loop twice and when the site grows could take a long time.

What is the best way of doing the above performance wise?

3

Try this method

Mage::getModel('catalog/category_api')->tree($parentID);

Make $parentID = null whenever you want to get all categories of root category.

It is given data in array for mat just like;

May this help you

Result:

Array
(
    [category_id] => 1
    [parent_id] => 0
    [name] => Root Catalog
    [is_active] => 
    [position] => 0
    [level] => 0
    [children] => Array
        (
            [0] => Array
                (
                    [category_id] => 2
                    [parent_id] => 1
                    [name] => Default Category
                    [is_active] => 1
                    [position] => 1
                    [level] => 1
                    [children] => Array
                        (
                            [0] => Array
                                (
                                    [category_id] => 4
                                    [parent_id] => 2
                                    [name] => Women
                                    [is_active] => 1
                                    [position] => 2
                                    [level] => 2
                                    [children] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [category_id] => 10
                                                    [parent_id] => 4
                                                    [name] => New Arrivals
                                                    [is_active] => 1
                                                    [position] => 1
                                                    [level] => 3
                                                    [children] => Array
                                                        (
                                                        )

                                                )

                                            [1] => Array
                                                (
                                                    [category_id] => 11
                                                    [parent_id] => 4
                                                    [name] => Tops & Blouses
                                                    [is_active] => 1
                                                    [position] => 2
                                                    [level] => 3
                                                    [children] => Array
                                                        (
                                                        )

                                                )

                                            [2] => Array
                                                (
                                                    [category_id] => 12
                                                    [parent_id] => 4
                                                    [name] => Pants & Denim
                                                    [is_active] => 1
                                                    [position] => 3
                                                    [level] => 3
                                                    [children] => Array
                                                        (
                                                        )

                                                )

                                            [3] => Array
                                                (
                                                    [category_id] => 13
                                                    [parent_id] => 4
                                                    [name] => Dresses & Skirts
                                                    [is_active] => 1
                                                    [position] => 4
                                                    [level] => 3
                                                    [children] => Array
                                                        (
                                                        )

                                                )

                                        )

                                )

                            [1] => Array
                                (
                                    [category_id] => 5
                                    [parent_id] => 2
                                    [name] => Men
                                    [is_active] => 1
                                    [position] => 3
                                    [level] => 2
                                    [children] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [category_id] => 14
                                                    [parent_id] => 5
                                                    [name] => New Arrivals
                                                    [is_active] => 1
                                                    [position] => 1
                                                    [level] => 3
                                                    [children] => Array
                                                        (
                                                        )

                                                )

                                            [1] => Array
                                                (
                                                    [category_id] => 15
                                                    [parent_id] => 5
                                                    [name] => Shirts
                                                    [is_active] => 1
                                                    [position] => 2
                                                    [level] => 3
                                                    [children] => Array
                                                        (
                                                        )

                                                )

                                            [2] => Array
                                                (
                                                    [category_id] => 16
                                                    [parent_id] => 5
                                                    [name] => Tees, Knits and Polos
                                                    [is_active] => 1
                                                    [position] => 3
                                                    [level] => 3
                                                    [children] => Array
                                                        (
                                                        )

                                                )

                                            [3] => Array
                                                (
                                                    [category_id] => 17
                                                    [parent_id] => 5
                                                    [name] => Pants & Denim
                                                    [is_active] => 1
                                                    [position] => 4
                                                    [level] => 3
                                                    [children] => Array
                                                        (
                                                        )

                                                )

                                            [4] => Array
                                                (
                                                    [category_id] => 40
                                                    [parent_id] => 5
                                                    [name] => Blazers
                                                    [is_active] => 1
                                                    [position] => 5
                                                    [level] => 3
                                                    [children] => Array
                                                        (
                                                        )

                                                )

                                        )

                                )

                            [2] => Array
                                (
                                    [category_id] => 6
                                    [parent_id] => 2
                                    [name] => Accessories
                                    [is_active] => 1
                                    [position] => 4
                                    [level] => 2
                                    [children] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [category_id] => 18
                                                    [parent_id] => 6
                                                    [name] => Eyewear
                                                    [is_active] => 1
                                                    [position] => 1
                                                    [level] => 3
                                                    [children] => Array
                                                        (
                                                        )

                                                )

                                            [1] => Array
                                                (
                                                    [category_id] => 19
                                                    [parent_id] => 6
                                                    [name] => Jewelry
                                                    [is_active] => 1
                                                    [position] => 2
                                                    [level] => 3
                                                    [children] => Array
                                                        (
                                                        )

                                                )

                                            [2] => Array
                                                (
                                                    [category_id] => 20
                                                    [parent_id] => 6
                                                    [name] => Shoes
                                                    [is_active] => 1
                                                    [position] => 3
                                                    [level] => 3
                                                    [children] => Array
                                                        (
                                                        )

                                                )

                                            [3] => Array
                                                (
                                                    [category_id] => 21
                                                    [parent_id] => 6
                                                    [name] => Bags & Luggage
                                                    [is_active] => 1
                                                    [position] => 4
                                                    [level] => 3
                                                    [children] => Array
                                                        (
                                                        )

                                                )

                                        )

                                )

                            [3] => Array
                                (
                                    [category_id] => 7
                                    [parent_id] => 2
                                    [name] => Home & Decor
                                    [is_active] => 1
                                    [position] => 5
                                    [level] => 2
                                    [children] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [category_id] => 22
                                                    [parent_id] => 7
                                                    [name] => Books & Music
                                                    [is_active] => 1
                                                    [position] => 1
                                                    [level] => 3
                                                    [children] => Array
                                                        (
                                                        )

                                                )

                                            [1] => Array
                                                (
                                                    [category_id] => 23
                                                    [parent_id] => 7
                                                    [name] => Bed & Bath
                                                    [is_active] => 1
                                                    [position] => 2
                                                    [level] => 3
                                                    [children] => Array
                                                        (
                                                        )

                                                )

                                            [2] => Array
                                                (
                                                    [category_id] => 24
                                                    [parent_id] => 7
                                                    [name] => Electronics
                                                    [is_active] => 1
                                                    [position] => 3
                                                    [level] => 3
                                                    [children] => Array
                                                        (
                                                        )

                                                )

                                            [3] => Array
                                                (
                                                    [category_id] => 25
                                                    [parent_id] => 7
                                                    [name] => Decorative Accents
                                                    [is_active] => 1
                                                    [position] => 4
                                                    [level] => 3
                                                    [children] => Array
                                                        (
                                                        )

                                                )

                                        )

                                )

                            [4] => Array
                                (
                                    [category_id] => 8
                                    [parent_id] => 2
                                    [name] => Sale
                                    [is_active] => 1
                                    [position] => 6
                                    [level] => 2
                                    [children] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [category_id] => 26
                                                    [parent_id] => 8
                                                    [name] => Women
                                                    [is_active] => 1
                                                    [position] => 1
                                                    [level] => 3
                                                    [children] => Array
                                                        (
                                                        )

                                                )

                                            [1] => Array
                                                (
                                                    [category_id] => 27
                                                    [parent_id] => 8
                                                    [name] => Men
                                                    [is_active] => 1
                                                    [position] => 2
                                                    [level] => 3
                                                    [children] => Array
                                                        (
                                                        )

                                                )

                                            [2] => Array
                                                (
                                                    [category_id] => 28
                                                    [parent_id] => 8
                                                    [name] => Accessories
                                                    [is_active] => 1
                                                    [position] => 3
                                                    [level] => 3
                                                    [children] => Array
                                                        (
                                                        )

                                                )

                                            [3] => Array
                                                (
                                                    [category_id] => 29
                                                    [parent_id] => 8
                                                    [name] => Home & Decor
                                                    [is_active] => 1
                                                    [position] => 4
                                                    [level] => 3
                                                    [children] => Array
                                                        (
                                                        )

                                                )

                                        )

                                )

                            [5] => Array
                                (
                                    [category_id] => 9
                                    [parent_id] => 2
                                    [name] => VIP
                                    [is_active] => 1
                                    [position] => 7
                                    [level] => 2
                                    [children] => Array
                                        (
                                        )

                                )

                        )

                )

        )

)
| improve this answer | |
  • 1
    awsome!!! its really helpfull – SagarPPanchal Nov 27 '15 at 13:18

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.