Adding Font Awesome Icons to Magento Top Links
As an experienced developer working with Magento templates, I often encounter requests like yours - adding custom icons from Font Awesome to top links in the boilerplate template. Since you already have Font Awesome included and are using a child theme based on the boilerplate, you’re halfway there!
Understanding Your Template Structure
The top.links.phtml file is responsible for rendering all the top links in your Magento storefront. The provided code snippet shows how it currently loops through each top link item ($toplinks) and renders them as <li> elements inside an unordered list.
<?php if($toplinks && is_array($toplinks)): ?>
<ul class="links"><?php echo $this->getChildHtml() ?>
<?php foreach($toplinks as $_toplink): ?>
<li<?php if($_toplink['first']||$_toplink['last']): ?> class="<?php if($_toplink['first']): ?>first<?php endif; ?><?php if($_toplink['last']): ?> last<?php endif; ?>"<?php echo $_toplink['liParams'] ?>>
<?php echo $_toplink['beforeText'] ?>
<a <?php echo $_toplink['aParams'] ?>><?php echo $_toplink['innerText'] ?></a>
<?php echo $_toplink['afterText'] ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Adding the Font Awesome Icon
To add the cart icon from Font Awesome, you’ll need to modify this loop. Since your question mentions adding the code before “My Cart (2 items)”, I’m assuming you want to prepend an icon to the ‘Cart’ link.
The font awesome icon for a cart is ïµ. You can include it using the <span> HTML element with the appropriate class. Update your loop as follows:
<?php if($toplinks && is_array($toplinks)): ?>
<ul class="links"><?php echo $this->getChildHtml() ?>
<?php foreach($toplinks as $_toplink): ?>
<li<?php if($_toplink['first']||$_toplink['last']): ?> class="<?php if($_toplink['first']): ?>first<?php endif; ?><?php if($_toplink['last']): ?> last<?php endif; ?>"<?php echo $_toplink['liParams'] ?>>
<?php if($_toplink['innerText'] === 'Cart'): ?>
<span class="fa fa-shopping-cart"></span>
<?php endif; ?>
<?php echo $_toplink['beforeText'] ?>
<a <?php echo $_toplink['aParams'] ?>><?php echo $_toplink['innerText'] ?></a>
<?php echo $_toplink['afterText'] ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
This code snippet checks if the inner text of the top link is ‘Cart’. If it is, a span element with the appropriate Font Awesome classes (fa fa-shopping-cart) is added before the link.
Conclusion
Adding custom icons to Magento top links can enhance your store’s user experience. By following this approach, you’ve successfully integrated the cart icon into your top links using Font Awesome.
Remember, always back up your theme files before making any changes. If you’re looking for more advanced customization options or facing other issues with your Magento development, feel free to explore our comprehensive guides on Laravel Company.
Happy coding!