Magento - Menu categorieŽn


Wil je zelf een menu plaatsen met alle hoofdcategorieŽn inclusief alle subcategorieŽn op het eerste niveau dan kan de onderstaande code worden gebruikt in de template.

<?php
        $_helper = Mage::helper('catalog/category');
        $_categories = $_helper->getStoreCategories();
        $currentUrl = $this->helper('core/url')->getCurrentUrl();
        echo '<ul>';
                foreach($_categories as $_category):
                        $_category = Mage::getModel('catalog/category')->load($_category->getId());
                        if ($_category->getUrl() == $currentUrl):
                                $selected = "selected";
                        else:
                                $selected = "not-selected";
                        endif;
                        if(strpos($currentUrl,$_category->getUrl_key()) !== false):
                                $selected = "selected";
                        endif;
                        if(!$_category->hasChildren()):
                                echo '<li class="' . $selected . '" ><a href="'. $_category->getUrl() . '">' . $_category->getName() . '</a></li>';
                        else:
                                echo '<li class="' . $selected . '" ><a href="'. $_category->getUrl() . '">' . $_category->getName() . '</a>';
                                        echo '<ul>';
                                                foreach($_category->getChildrenCategories() as $_subCategory):
                                                        $_subCategory = Mage::getModel('catalog/category')->load($_subCategory->getId());
                                                        if(!$_subCategory->getIncludeInMenu()):
                                                        else:
                                                                echo '<li><a href="' . $_subCategory->getUrl() . '">' . $_subCategory->getName() . '</a></li>';
                                                        endif;
                                                endforeach;
                                        echo '</ul>';
                                echo '</li>';
                        endif;
                endforeach;
        echo '</ul>';
?>


Aanvullend kan middels een kleine aanpassing een menu worden geplaatst op basis van ID. Vul dan het ID in van de categorie waarvan je de subcategorieŽn wilt gaan tonen.

<?php
        $_categories = Mage::getModel('catalog/category')->getCategories(10);
        echo '<ul>';
                foreach($_categories as $_category):
                        $_category = Mage::getModel('catalog/category')->load($_category->getId());
                        if ($_category->getUrl() == $currentUrl):
                                $selected = "selected";
                        else:
                                $selected = "not-selected";
                        endif;
                        if(strpos($currentUrl,$_category->getUrl_key()) !== false):
                                $selected = "selected";
                        endif;
                        if(!$_category->hasChildren()):
                                echo '<li class="' . $selected . '" ><a href="'. $_category->getUrl() . '">' . $_category->getName() . '</a></li>';
                        else:
                                echo '<li class="' . $selected . '" ><a href="'. $_category->getUrl() . '">' . $_category->getName() . '</a>';
                                        echo '<ul>';
                                                foreach($_category->getChildrenCategories() as $_subCategory):
                                                        $_subCategory = Mage::getModel('catalog/category')->load($_subCategory->getId());
                                                        if(!$_subCategory->getIncludeInMenu()):
                                                        else:
                                                                echo '<li><a href="' . $_subCategory->getUrl() . '">' . $_subCategory->getName() . '</a></li>';
                                                        endif;
                                                endforeach;
                                        echo '</ul>';
                                echo '</li>';
                        endif;
                endforeach;
        echo '</ul>';
?>