Magento discount labels - getting special price on category page

Magento discount labels - getting special price on category page

September 4, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento discount labels - getting special price on category page

Displaying Discount Badges on Magento Category Pages

Magento provides robust functionality for pricing and promotions, but sometimes you may need to customize how discounts are displayed across different pages of your store. A common scenario is displaying discount badges or labels on category pages that show the percentage savings for each product.

In this post, we’ll explore a solution to display accurate discount percentages on Magento category pages using the existing price block and some custom code. The key is to understand how the price block works and make modifications only where necessary to avoid affecting other parts of your store’s pricing logic.

Understanding the Price Block

By default, Magento uses a single price block for both product detail pages and category listings. This block is responsible for displaying regular prices, special prices, final prices, and tax information. It’s designed to be flexible and reusable across different contexts within the storefront.

When you call <?php echo $this->getPriceHtml($_product, true) ?> on a category page, it renders the entire price block with all its built-in functionality for that specific product. This is why modifying the core price rendering logic can have unintended consequences on other parts of your store.

Customizing Discount Labels Without Affecting Product Pages

To display discount badges only on the category pages without breaking the pricing logic on product detail pages, you’ll need to create a custom extension that augments the existing price block functionality. Here’s an overview of the approach:

  1. Create a new module: Start by creating a new Magento module to house your customizations. This will keep your changes separate from the core codebase and make it easier to manage updates.

  2. Extend the core price block: In your custom module, create a new price block that extends the core Magento\Catalog\Block\Product\Price class. Override only the necessary methods to add your discount label functionality.

  3. Calculate the discount percentage: Within your custom price block, calculate the discount percentage for each product using the regular price and special price values. Ensure you’re using the correct Magento models and methods to fetch these prices accurately.

  4. Render the discount label: Once you have the discount percentage calculated, use a conditional statement within the price rendering process to display your custom discount badge only when the percentage is greater than zero. You can create a template file for the discount badge in your module’s view directory.

  5. Apply the changes: Update your category page layout XML files (e.g., catalog_category_view.xml) to include your new custom price block instead of the default one. This ensures that only your category listings use the modified functionality, leaving product detail pages unchanged.

Example Code Snippets

Here’s an example of how you might calculate and display a discount percentage within a custom price block:

class CustomPrice extends \Magento\Catalog\Block\Product\Price
{
    public function getDiscountPercentage($product)
    {
        $regularPrice = $product->getPrice();
        $specialPrice = $product->getSpecialPrice();

        if ($specialPrice && $specialPrice < $regularPrice) {
            $discountAmount = $regularPrice - $specialPrice;
            $discountPercentage = (($discountAmount / $regularPrice) * 100);
            return round($discountPercentage, 0);
        }

        return 0;
    }

    public function renderLabel()
    {
        $html = '';
        foreach ($this->getItems() as $_product) {
            // ... existing price rendering logic ...

            if ($_product->getData('special_price') && $_product->getData('price') != $_product->getFinalPrice()) {
                $specialPercent = round(100 - ((100 / $_product->getRegularPrice()) * $_product->getFinalPrice()), 0);
                if ($specialPercent != 0) {
                    $html .= '<span class="discount-label">' . $specialPercent . '% off</span>';
                }
            }

            // ... continue with existing price rendering logic ...
        }

        return $html;
    }
}

In this example, the getDiscountPercentage method calculates the discount percentage for a given product, while the renderLabel method checks if there’s a special price and renders a custom label only when the discount is non-zero.

Conclusion

Displaying accurate discount badges on Magento category pages requires careful customization of the price block functionality. By creating a new module and extending the core price block with minimal modifications, you can achieve your goal without affecting the pricing logic on product detail pages.

Remember to test your changes thoroughly in a staging environment before deploying them to your live storefront. Additionally, consider using Magento’s built-in promotional pricing rules for more sophisticated discount scenarios, as they are designed to be flexible and easy to manage.

For further guidance on developing custom Magento extensions and optimizing your store’s performance, visit LaravelCompany to explore our range of Magento development services.

Catalog Pricing

Our network