Magento: Getting Discounted price

Magento: Getting Discounted price

July 1, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento: Getting Discounted price

Handling Discounted Prices in Magento: A Comprehensive Guide

As an experienced Magento developer, I often encounter clients who need to display discounted prices on their frontend for products that use both specialPrice and catalog rules. In this post, we’ll explore how to accurately retrieve the final discounted price for all your products regardless of the pricing method used.

Understanding the Problem: $_product->getSpecialPrice()

The getSpecialPrice() method in Magento 2 is designed to handle special prices set at the product level. However, it fails when a product’s price is determined by catalog rules instead of the special price attribute:

$specialPrice = $_product->getSpecialPrice();

This approach works fine for products with special prices, but for those that use catalog rules, you’ll encounter empty strings or NaN values.

The Solution: Using Price Helpers

Magento provides a robust set of helper classes to handle pricing logic. By using the price helpers, we can ensure that both special prices and catalog rule prices are accurately retrieved.

First, let’s fetch the final product price including all discounts:

$finalPrice = $product->getFinalPrice();

The getFinalPrice() method combines the product’s regular price with any applicable discounts from either special prices or catalog rules. It returns the final sale price that should be displayed to customers.

Displaying the Discount Percentage

Now, let’s calculate and display the discount percentage based on the final price:

$regularPrice = $product->getPrice();
$salePrice = $product->getFinalPrice();

if ($salePrice < $regularPrice) {
    $discountPercent = (($regularPrice - $salePrice) / $regularPrice) * 100;
    printf("Discount: %.2f%%", $discountPercent);
}

In this example, we first retrieve the regular price and final sale price. If the sale price is lower than the regular price, we calculate the discount percentage by comparing the difference between them.

Handling Catalog Rules

Magento’s catalog rules are applied after tax calculation if applicable. To ensure that you’re displaying the correct discounted price, make sure to apply any necessary tax calculations:

$finalPrice = $product->getFinalPrice();
if ($this->_taxHelper->priceIncludesTax($_product)) {
    // If the price includes tax, calculate the final price after tax is applied
    $finalPrice = $this->_taxHelper->getPrice(
        $_product,
        $_product->getFinalPrice(),
        false
    );
}

Best Practices

  1. Always use the getFinalPrice() method for retrieving sale prices to ensure compatibility with both special prices and catalog rules.

  2. Apply tax calculation if necessary, especially when dealing with products that have prices inclusive of tax.

  3. Consider using Magento’s price helper classes (\Magento\Framework\Pricing\Helper\Data) for more advanced pricing requirements.

Conclusion

In this post, we’ve explored how to accurately retrieve and display discounted prices in Magento 2 for products using both special prices and catalog rules. By leveraging the built-in getFinalPrice() method and price helper classes, you can ensure that your frontend displays the correct sale prices across all product types.

Remember, understanding Magento’s pricing architecture is key to delivering accurate information to your customers. Always test your implementations thoroughly to ensure they work as expected across different scenarios.

For more in-depth guides on Magento development and best practices, feel free to explore our blog at https://laravelcompany.com. We regularly publish articles covering the latest trends and tips for building successful e-commerce platforms with Magento.

Magento 2 Catalog Themes Customers

Our network