Use Font Awesome star rating for radio buttons

Use Font Awesome star rating for radio buttons

July 14, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Use Font Awesome star rating for radio buttons

Enhancing Magento Product Rating: Implementing Font Awesome Star Ratings

As an e-commerce store owner or developer, you’re always looking for ways to improve user experience and engagement on your website. One key area is the product review and rating system. By default, Magento uses standard radio buttons for star ratings, which can feel dull and uninspiring compared to modern UI/UX practices.

In this post, we’ll walk through a step-by-step guide to replacing those boring radio buttons with sleek Font Awesome star icons that dynamically fill based on the selected rating. This will not only enhance the visual appeal of your product reviews but also provide a more intuitive and engaging user interface for your customers.

Understanding the Problem

You’re currently trying to integrate Font Awesome CSS into Magento’s dynamic star rating system, which is built using PHP loops and HTML markup. The default code snippet looks like this:

<tr>
    <th><?= $this->escapeHtml($_rating->getRatingCode()) ?></th>
<?php foreach ($_rating->getOptions() as $_option): ?>
    <td class="value">
        <input type="radio" name="ratings[<?= $_rating->getId() ?>]" id="<?= $this->escapeHtml($_rating->getRatingCode()) ?>_<?= $_option->getValue() ?>" value="<?= $_option->getId() ?>" class="radio" />
        <label class = "full" for="<?= $this->escapeHtml($_rating->getRatingCode()) ?>_<?= $_option->getValue() ?>"></label>
    </td>
<?php endforeach; ?>
</tr>

While you’ve attempted to use a JSFiddle example (https://jsfiddle.net/tLj2ybnu/7/) and another codepen (http://codepen.io/jamesbarnett/pen/vlpkh) as references, you haven’t been able to successfully integrate the Font Awesome star rating system that fills based on the selected review.

The Solution: Customizing Magento for Font Awesome Star Ratings

To achieve this, we’ll need to make a few changes to your Magento theme and add some custom CSS and JavaScript. Here’s a step-by-step approach:

  1. Add Font Awesome CSS: First, ensure that you have the Font Awesome library included in your theme’s layout or page template. You can either download it directly from their website (https://fontawesome.com/) or use a CDN like this:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta2/css/all.min.css">

Place this line within the <head> section of your theme’s layout file, typically located at app/design/frontend/{package}/{theme}/Magento_Theme/templates/html/head.phtml.

  1. Update the Rating Template: Next, we’ll modify the rating template to use Font Awesome icons instead of radio buttons and labels. Update the relevant PHP and HTML code in your rating template (usually found under vendor/magento/module-theme/view/frontend/templates/rating.phtml or a custom copy within your theme):
<div class="rating-form__list">
    <php foreach ($_rating->getOptions() as $_option): ?>
        <div class="rating-item">
            <input type="radio" name="ratings[<?= $_rating->getId() ?>]" id="<?= $this->escapeHtml($_rating->getRatingCode()) ?>_<?= $_option->getValue() ?>" value="<?= $_option->getId() ?>" class="hidden radio" />
            <label class="rating-item__icon" for="<?= $this->escapeHtml($_rating->getRatingCode()) ?>_<?= $_option->getValue() ?>">
                <i class="fas fa-star"></i>
                <span class="sr-only"><?= $this->escapeHtml(__('Rate %1', $_option->getLabel())) ?></span>
            </label>
        </div>
    <?php endforeach; ?>
</div>

We’ve replaced the radio buttons with hidden input fields and used Font Awesome’s fas fa-star class for the star icon. The label now wraps around the icon.

  1. Add Custom JavaScript: To make the stars fill based on the selected review, we need to add a small piece of JavaScript that updates the star icons dynamically when a rating is chosen. Create a new JavaScript file (e.g., star-rating.js) and place it under your theme’s JavaScript directory (app/design/frontend/{package}/{theme}/web/js).
require(['jquery'], function($) {
    $(document).ready(function() {
        $('.rating-item').click(function() {
            var ratingValue = $(this).find('input[type="radio"]').val();
            $(this).parents('.rating-form').find('.rating-item i').removeClass('fas fa-star');
            $(this).parents('.rating-form').find('.rating-item i').addClass('far fa-star');
            for (var i = 1; i <= ratingValue; i++) {
                $(this).siblings().eq(i - 1).find('i').addClass('fas fa-star');
            }
        });
    });
});
  1. Include the JavaScript File: Finally, include the star-rating.js file in your theme’s layout file (usually within the <head> section):
<script src="<?= $this->getViewFileUrl('js/star-rating.js') ?>"></script>

Conclusion

By following these steps and updating your Magento theme as described, you’ll successfully enhance your product rating system with dynamic Font Awesome star icons. This will not only improve the visual appeal of your reviews but also provide a more intuitive and engaging user experience for your customers.

Remember to test your changes thoroughly after implementation to ensure compatibility across different browsers and devices. If you encounter any issues or need further assistance, feel free to reach out to our professional Magento development services at Laravel Company.

Happy coding, and happy enhancing!

Catalog Themes Customers Errors

Our network