Adding a Shopping Cart Icon to Magento Top-Links
As your Magento store grows and customization requirements increase, you may need to make adjustments to the top-links menu without relying solely on CSS. In this post, we’ll explore the best practices for editing Magento’s top-links, focusing on adding a shopping cart icon before the “My Cart” link.
Understanding Magento Top-Links
Magento’s top-links are rendered by the app/design/frontend/base/default/template/page/html/toplinks.phtml template file. These links are generated dynamically based on the layout XML defined in your theme or module.
The top-links are controlled by layout update XML files, typically located at app/design/frontend/{package}/{theme}/layout/local.xml. The relevant section for the top-links is usually found under <default> or a specific page type like catalog_product_view.
Option 1: Manipulating topmenu.phtml
The toplinks.phtml file controls the rendering of all top-links. However, directly modifying this core template file is not recommended as it can be overwritten during updates.
To safely make changes to the top-links without touching the core files:
- Copy the
app/design/frontend/base/default/template/page/html/toplinks.phtmlfile to your theme directory:app/design/frontend/{package}/{theme}/template/page/html/toplinks.phtml. - Open the copied file and look for the block where the “My Cart” link is generated (usually inside a foreach loop).
Let’s assume the relevant code snippet looks like this:
<?php if ($this->getChild('topcart')->getLayout()->getBlock('topcart')->isVisible()): ?>
<li>
<?php echo $this->getChildHtml('topcart'); ?>
</li>
<?php endif; ?>
Option 2: Injecting a New Block
To add the shopping cart icon without modifying the core code, you can create a new block and inject it before the “My Cart” link. Here’s how to do this:
- In your theme layout file (
app/design/frontend/{package}/{theme}/layout/local.xml), find or add the following section under<default>or the specific page type:
<cms_index_index>
<reference name="top.links">
<block type="core/text_list" name="custom.toplinks.icon" as="custom.toplinks.icon">
<action method="setInnerHtml"></action>
<action method="addText"><text><![CDATA[<img src="{{skin url='images/shoppingcart.png'}}" alt="Shopping Cart">]]></text></action>
</block>
</reference>
</cms_index_index>
- In the same layout file, add a new reference to move the “My Cart” link after your custom icon block:
<move element="topCart" destination="custom.toplinks.icon" after="-">
Option 3: Using XML Files
You can also use layout XML files to add the shopping cart icon without modifying any PHP templates. This approach provides better code separation and maintainability.
-
Copy or create a new file at
app/design/frontend/{package}/{theme}/layout/local.xml. -
Add the following block definition and reference:
<cms_index_index>
<reference name="top.links">
<!-- New block for custom icon -->
<block type="core/text_list" name="custom.toplinks.icon" as="custom.toplinks.icon">
<action method="setInnerHtml"></action>
<action method="addText"><text><![CDATA[<img src="{{skin url='images/shoppingcart.png'}}" alt="Shopping Cart">]]></text></action>
</block>
<!-- Move the 'My Cart' link after the custom icon -->
<move element="topCart" destination="custom.toplinks.icon" after="-">
</reference>
</cms_index_index>
Option 4: Using Magento’s XML Layout Update
You can also use Magento’s built-in XML layout update functionality to achieve this without touching any PHP files or creating custom blocks.
-
Create a new file at
app/design/frontend/{package}/{theme}/layout/local.xml. -
Add the following layout update:
<cms_index_index>
<update handle="catalog_toplinks">
<!-- Insert the icon before the 'My Cart' link -->
<reference name="top.links">
<action method="addLink" ifconfig="catalog/toplink_shopping_cart/enabled">Shopping Cart</action>
<block type="core/text_list" name="custom.toplinks.icon" as="custom.toplinks.icon">
<action method="setInnerHtml"></action>
<action method="addText"><text><![CDATA[<img src="{{skin url='images/shoppingcart.png'}}" alt="Shopping Cart">]]></text></action>
</block>
<!-- Move the 'My Cart' link after the custom icon -->
<move element="topCart" destination="custom.toplinks.icon" after="-">
</reference>
</update>
</cms_index_index>
Conclusion
Adding a shopping cart icon to Magento’s top-links without using CSS involves understanding and manipulating Magento’s layout XML files or creating new blocks through PHP templates. The best approach depends on your specific requirements, but using layout XML updates or custom blocks injected via layout XML provides the most maintainable and performant solutions.
Remember that directly modifying core template files should be avoided to prevent conflicts during updates. Always create copies of core templates in your theme directory when making changes.
For more advanced Magento customization techniques and best practices, consider exploring our comprehensive guide on Magento Theme Development.
Happy coding!