branko ajzele, senior developer / project manager

Posts / Articles

Custom category menu navigation in Magento

Just some code for custom category menu navigation. I’m a bit short on time so, no fancy walk trough. All you need to make this show on your page is to call it inside layout file like

<block type=”core/template” template=”surgeworks/customcatnav.php” />

Filename: customcatnav.php.

<?php
//Get store id
$storeId    = Mage::app()->getStore()->getId();

//Get category model
$_category = Mage::getModel(‘catalog/category’)->setStoreId($storeId);

$_categoryCollection = $_category->getCollection();
$_categoryCollectionIds = $_categoryCollection->getAllIds();

//Remove root category from array
unset($_categoryCollectionIds[0], $_categoryCollectionIds[1]);

?>

<div class=”content”>
<?php

$o = null;

$o .= ‘<dl>’;
foreach ($_categoryCollectionIds as $catId)
{
$_category = $_category->load($catId);

if($_category->getLevel() == 2)
{

$catChildren = $_category->getChildren();
if(!empty($catChildren))
{
$o .= ‘<dt>’.$_category->getName().’</dt>’;

$categoryChildren = explode(“,”, $catChildren);

foreach ($categoryChildren as $categoryChildId)
{
/* @var $_childCategory Mage_Catalog_Model_Category */
$_childCategory = $_category = Mage::getModel(‘catalog/category’)->setStoreId($storeId)->load($categoryChildId);

$o .= ‘<dd>’;
$o .= ‘<span class=”categoryName”><a href=”‘.$_childCategory->getUrl().’”>’.$_childCategory->getName().’</span class=”totalNumberOfProducts”></a>’;
$o .= ‘<span class=”totalNumberOfProducts”>’.$_childCategory->getProductCollection()->count().’</span>’;
$o .= ‘</dd>’;
}

}

}
}
$o .= ‘</dl>’;

echo $o;

?>
</div><?php //content ?>

  • Antonio

    Kolegstvo pozz..
    Svaka cast, raspistoljio se ti s kodom;)
    Cujemo se..

  • http://nop Antonio

    Kolegstvo pozz..
    Svaka cast, raspistoljio se ti s kodom;)
    Cujemo se..

  • Joan

    Its posible to roder the category collection by name?

  • Joan

    Its posible to roder the category collection by name?

  • ashish

    can u plz help me, i m tryin to import config products using csv file but i dont know how to do it? simple products can easilt be imported but config …. i m tired of thinking for those

  • ashish

    can u plz help me, i m tryin to import config products using csv file but i dont know how to do it? simple products can easilt be imported but config …. i m tired of thinking for those

  • zmove

    Hi,

    In your code, if you have several root category for several websites, it print them all. How to restrict the category for a special category root, the category root set up for the current store ?

  • zmove

    Hi,

    In your code, if you have several root category for several websites, it print them all. How to restrict the category for a special category root, the category root set up for the current store ?

  • canario_aleman

    Hi Branko,

    first of all I’d like to thank you for sharing your experience with the community.

    Your example for a custom category menu however is lacking one or two important lines of code (depending on the implementation ;) ):

    In the last foreach loop you should check $_childCategory->getIsActive() in order to skip categories deactivated in the backend.

    Kind regards,

    Frank

  • canario_aleman

    Hi Branko,

    first of all I’d like to thank you for sharing your experience with the community.

    Your example for a custom category menu however is lacking one or two important lines of code (depending on the implementation ;) ):

    In the last foreach loop you should check $_childCategory->getIsActive() in order to skip categories deactivated in the backend.

    Kind regards,

    Frank

  • monocat

    Great work. I tested it and it works. For people having view issues, don’t forget to add “getChildHtml(‘customcatnav’) ?>”. In my case I added it under header.phtml.

    I do have a question. What’s the simplest form to call upon a specific subCategory Id. I would like to only list a specific category’s sub categories and nothing else.

    Thanks in advance.

  • monocat

    Great work. I tested it and it works. For people having view issues, don’t forget to add “getChildHtml(‘customcatnav’) ?>”. In my case I added it under header.phtml.

    I do have a question. What’s the simplest form to call upon a specific subCategory Id. I would like to only list a specific category’s sub categories and nothing else.

    Thanks in advance.

  • monocat

    Sorry. Looks like the code got cut off. Here it is:
    getChildHtml('customcatnav') ?>

  • monocat

    Sorry. Looks like the code got cut off. Here it is:
    getChildHtml('customcatnav') ?>

  • mik

    this script has worked well for me over the course of developing a site that is soon to launch, however:

    i just upgraded to magento 1.3.1, and now, the sub categories will not reorder, when i change their order on the ‘manage categories’ page in the admin.

    any ideas?

  • mik

    this script has worked well for me over the course of developing a site that is soon to launch, however:

    i just upgraded to magento 1.3.1, and now, the sub categories will not reorder, when i change their order on the ‘manage categories’ page in the admin.

    any ideas?

  • http://activecodeline.com/building-magento-business-by-stealing-stuff-asia-connect-com-vn-knows-how/ Building Magento business by stealing stuff, asia-connect.com.vn knows how | ActiveCodeline

    [...] Here is the original. [...]

  • anonymus

    thanks :)

  • anonymus

    thanks :)

  • kk

    How to get all childrent store ( level 2,3,4… )

blog comments powered by Disqus