[Laravel Company Blog Post]
Adding an Image Icon Next to Magento Category Menu Names
In this tutorial, we’ll guide you through adding a small image icon next to each category and sub-category name in your Magento store’s menu. We won’t replace the category name with an image; instead, we’ll place an image after it.
The Challenge: Overriding app/core/code/Mage/Catalog/Block/Navigation.php
You’ve already attempted overriding the _renderCategoryMenuItemHtml method in app/core/code/Mage/Catalog/Block/Navigation.php. You changed the code to add a CSS class based on the category ID:
$htmlLi .= '>';
$html[] = $htmlLi;
$html[] = '<a href="'.$this->getCategoryUrl($category).'"'.$linkClass.'><span class="category_'.$this->getCurrentCategory()->getId().'">'.$this->escapeHtml($category->getName()).'</span></a>';
Then, you tried referencing this category ID in your styles.css to add a background image. However, this approach didn’t work as expected.
The Solution: Using Layout XML and Block Class Overrides
Instead of directly modifying the core files (which is not recommended for upgrades), we’ll use a combination of layout XML updates and block class overrides.
- Create a new local extension.
- In your module’s
etc/config.xml, declare the dependencies:
<config>
<modules>
<Namespace_Module>
<version>0.1.0</version>
</Namespace_Module>
</modules>
<global>
<helpers>
<Namespace_Module>
<class>Namespace_Module_Helper_Data</class>
</Namespace_Module>
</helpers>
<blocks>
<catalog>
<rewrite>
<navigation>Namespace_Module_Block_Catalog_Navigation</navigation>
</rewrite>
</catalog>
</blocks>
</global>
</config>
- In your module’s
etc/catalog.xml, add the following layout updates to move the icon after the category name:
<layout version="1.0">
<default>
<reference name="catalog.topmenu" translate="label">
<action method="addLink" linkClass="level0">
<label>Category Menu</label>
<url> catalogue_category/view/key/{category_id}</url>
<title>Example Category</title>
<after><action method="addItem" translate="label"><menu_name><![CDATA[category_0]]></menu_name><label><![CDATA[Example Category]]></label><url><![CDATA[{{store direct_url="catalog_category/view"}}]]></url><level>0</level><parent_id>0</parent_id><position>1</position><category_id>3</category_id><is_active>1</is_active><is_anchor>0</is_anchor><sort_order>1524578961</sort_order></action></after>
</action>
</reference>
</default>
</layout>
- In your module’s
Block/Catalog/Navigation.php, add the icon after the category name:
public function _renderCategoryMenuItemHtml($item)
{
$html = '<a href="'.$this->getCategoryUrl($item).'"'.$this->getCategoryClass($item, 'level0').'><span>'.$this->escapeHtml($item->getName()).'</span><img src="'.$this->getImageUrl('category_icon.png').'" alt="Category Icon" /></a>';
return $html;
}
- In your module’s
etc/view.xml, define the category icon image:
<view>
<media>
<images>
<image id="category_icon">
<file>path/to/your/category_icon.png</file>
</image>
</images>
</media>
</view>
Remember to replace Namespace_Module with your actual module name and update the image path accordingly.
Conclusion
Adding an icon next to category names in Magento requires a combination of layout XML updates and block class overrides. By following this approach, you avoid directly modifying core files and maintain the ability to upgrade your platform safely.
For more advanced customizations like theme-specific adjustments or complex layout modifications, consider consulting with our experienced Magento developers at Laravel Company.
Contact us today to discuss your project requirements and receive expert guidance on Magento development best practices.