How to Get Category Thumbnails in Navbar Menus

extensionsimagemagento-1menunavigation

I have been very happy so far with "Responsive Custom Menu", however it does not have the option to pull in thumbnail images for categories and subcategories on the menu. I was about to extend it by adding a getThumbnail call to the getter and renderers for this navbar, but ran into some issues where it did not construct the url properly.

It seems that the main files to modify would be app/code/local/WP/CustomMenu/Block/Navigation.php and perhaps app/design/frontend/default/default/template/webandpeople/custommenu/top.phtml.

Can anyone provide some specific modifications that would insert the thumbnails into the menu for further styling?

Additionally, I'm fine with modifying this extension directly to provide these thumbnails, but would there be a better method to extend an extension such as this so that updates are still tenable and likely still work?

EDIT:

It seems that this may hold some keys here to implementing this properly: http://www.h-o.nl/blog/using_category_images_in_your_magento_navigation/

Best Answer

Override the observer

            <page_block_html_topmenu_gethtml_before>
            <observers>                    
                <catalog_add_topmenu_items>
                    <type>disabled</type>
                </catalog_add_topmenu_items>
                <custom_catalog_add_topmenu_items>
                    <class>custom_catalog/observer</class>
                    <method>addCatalogToTopmenuItems</method>
                </custom_catalog_add_topmenu_items>                    
            </observers>
        </page_block_html_topmenu_gethtml_before>           

Then in your observer, these three function. Modify to your needs.

    /**
 * Adds catalog categories to top menu
 *
 * @param Varien_Event_Observer $observer
 */
public function addCatalogToTopmenuItems(Varien_Event_Observer $observer)
{
    $block = $observer->getEvent()->getBlock();
    $block->addCacheTag(Mage_Catalog_Model_Category::CACHE_TAG);
    $this->_addCategoriesToMenu(
        Mage::helper('catalog/category')->getStoreCategories(), $observer->getMenu(), $block, true
    );
}

/**
 * Recursively adds categories to top menu
 *
 * @param Varien_Data_Tree_Node_Collection|array $categories
 * @param Varien_Data_Tree_Node $parentCategoryNode
 * @param Mage_Page_Block_Html_Topmenu $menuBlock
 * @param bool $addTags
 */
protected function _addCategoriesToMenu($categories, $parentCategoryNode, $menuBlock, $addTags = false)
{
    $categoryModel = Mage::getModel('catalog/category');
    foreach ($categories as $category) {
        if (!$category->getIsActive()) {
            continue;
        }

        $nodeId = 'category-node-' . $category->getId();

        $categoryModel->setId($category->getId());
        if ($addTags) {
            $menuBlock->addModelTags($categoryModel);
        }

        $tree = $parentCategoryNode->getTree();
        $categoryData = array(
            'name' => $category->getName(),
            'id' => $nodeId,
            'url' => Mage::helper('catalog/category')->getCategoryUrl($category),
            'is_active' => $this->_isActiveMenuCategory($category)
        );
        $categoryNode = new Varien_Data_Tree_Node($categoryData, 'id', $tree, $parentCategoryNode);
        $parentCategoryNode->addChild($categoryNode);

        $flatHelper = Mage::helper('catalog/category_flat');
        if ($flatHelper->isEnabled() && $flatHelper->isBuilt(true)) {
            $subcategories = (array)$category->getChildrenNodes();
        } else {
            $subcategories = $category->getChildren();
        }

        $this->_addCategoriesToMenu($subcategories, $categoryNode, $menuBlock, $addTags);
    }
}

/**
 * Checks whether category belongs to active category's path
 *
 * @param Varien_Data_Tree_Node $category
 * @return bool
 */
protected function _isActiveMenuCategory($category)
{
    $catalogLayer = Mage::getSingleton('catalog/layer');
    if (!$catalogLayer) {
        return false;
    }

    $currentCategory = $catalogLayer->getCurrentCategory();
    if (!$currentCategory) {
        return false;
    }

    $categoryPathIds = explode(',', $currentCategory->getPathInStore());
    return in_array($category->getId(), $categoryPathIds);
}