Display Regular Price, Special Price, and Percentage Discount on Magento 2 Product Page
Are you struggling to display both the regular price and special price of your configurable products in Magento 2? And how about showing the percentage discount applied? You’re not alone.
Understanding the Problem
The issue lies in the way Magento handles pricing for configurable products. By default, it only displays the final sale price and hides the original regular price behind a “Was” label or completely removes it from view. This can be confusing for customers who want to see the full picture of your pricing strategy.
The Current Approach
You’ve mentioned trying to modify the final_price.html file in your theme directory, which is a great starting point. However, this file only controls how the final sale price is rendered and doesn’t provide access to the regular price or discount information directly.
The Solution: Accessing Pricing Models
To solve this, we need to dive into Magento’s pricing models for configurable products. Each product has multiple pricing models, including a regular price model that stores the original base price before any discounts are applied.
Here’s a simplified example of how you can access these pricing models and render them on your product page:
// Get the regular price model for the current product
$regularPriceModel = $block->getPriceType('regular_price');
$minimalRegularPrice = $regularPriceModel->getMinimalPrice();
// Render the regular price with proper formatting
<span class="old-price">
<?php /* @escapeNotVerified */ echo $block->renderAmount($minimalRegularPrice, [
'display_label' => __('Regular Price'),
'price_id' => $block->getPriceId('old-price-' . $idSuffix),
'include_container' => true,
'skip_adjustments' => true
]); ?>
</span>
In this code snippet, we first retrieve the regular price model for the current product using the getPriceType method. We then call the getMinimalPrice method to get the minimum regular price across all options of a configurable product.
Next, we use the renderAmount method provided by the block to format and display this regular price on the page. The array passed as the second argument contains various configuration parameters for the rendering process.
Calculating and Displaying Discount Percentage
To calculate the percentage discount, you can subtract the final sale price from the regular price and divide the result by the original regular price:
// Calculate the discount amount
$discountAmount = $minimalRegularPrice - $minimalPrice;
// Calculate the discount percentage
$discountPercent = ($discountAmount / $minimalRegularPrice) * 100;
Then, you can display this percentage on your product page using the renderLabel method:
// Render the discount percentage with proper formatting
<span class="price-label">
<?php /* @escapeNotVerified */ echo $block->renderLabel($discountPercent . '%'); ?>
</span>
Putting It All Together
To implement this solution, you’ll need to modify or extend your current final_price.html template file. You can add the regular price and discount percentage rendering code wherever you want them to appear on the product page.
Remember that every store has its unique requirements and branding guidelines, so feel free to adjust the styling and placement of these pricing elements according to your specific needs.
Conclusion
Displaying both the regular price and special price along with the applied discount percentage can greatly enhance transparency for your customers. By leveraging Magento’s built-in pricing models and rendering methods, you can achieve this goal with minimal effort.
Don’t hesitate to explore more customization options in our Magento 2 Theme Development Guide to create an even more engaging shopping experience for your customers.