Magento – Show thumbnail images of sub category in top navigation magento 1.9.0

magento-1.9

I am trying to implement the solution detailed here:

How to show the sub category images in Top menu in magento 1.9?

I created a custom module based on the post. However, it brakes the site, I'm getting this error:

Mage registry key "_singleton/catalog/observer" already exists

Here is my file structure:

At app/code/local/Custom/CategoryImage/etc I have the following: config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Custom_CategoryImage>
            <version>1.0.0</version>
        </Custom_CategoryImage>
    </modules>
    <global>
        <models>
            <categoryImage>
                <class>Custom_CategoryImage_Model</class>
            </categoryImage>
            <catalog>
                <rewrite>
                    <observer>Custom_CategoryImage_Model_Observer</observer>
                </rewrite>
            </catalog>
        </models>
    </global>
</config>

At app/etc/modules have the following xml file: Custom_CategoryImage.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Custom_CategoryImage>
            <active>true</active>
            <codePool>local</codePool>
        </Custom_CategoryImage>
    </modules>
</config>

At app/code/local/Custom/CategoryImage/Model/Observer if have the following Observer.php

class Custom_CategoryImage_Model_Observer extends Mage_Catalog_Model_Observer {

    public function addCategoryImages(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
        );
    }

    public 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),
                'thumbnail' => $categoryModel->load($category->getId())->getThumbnail()
            );
            $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);
        }
    }

}

Best Answer

Your code is correct. Tested it locally without any problems.

Do it works I have a few small notes;

The public function addCategoryImages you use isn't defined anywhere in standard Magento as a observing callback.

The private function however is used in Magento. So you could override that function.

You could add a dependency so you know for sure that your config is loaded last.

<?xml version="1.0"?>
<config>
    <modules>
        <Custom_CategoryImages>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Catalog/>
            </depends>
        </Custom_CategoryImages>
    </modules>
</config>

How to proceed

So your current problem derives from somewhere else, maybe two rewrites on the same class? If another module is also rewriting catalog/observer it could be missing something.

Be sure all cache is disabled, that compilation is disabled and try again. Search for other catalog rewrites, disable some modules, etcetera.

Related Topic