1

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);
        }
    }

}

2 Answers 2

2

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.

1

You are overriding the observer wrong, The correct syntax to override the observer is <events> <event_name> <observers> <some_extension_observer> <type>singleton</type> <class>some_extension/observer</class> <method>someMethod</method> </some_extension_observer> </observers> </event_name> </events>

You can not override an observer inside the <model></model>

1
  • That's not entirely true, you can in fact. But you have to know for sure that the observer you are rewriting is loaded by catalog/observer if it is configured and called Mage_Catalog_Model_Observer::functionCall you cannot.
    – Jeroen
    Commented Mar 13, 2015 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.