Returning customers are like godsends for the store owner. Because they already have shown interest in your store products and willing to spend money on your products that’s why its time to convert them by offering some great discount on store product.
In fact, most of the online stores typically get 50% of their revenue from their existing store customers. To make them feel special by offering discount can drastically help a lot by setting up a special price inside the store.
The customer always looks convenience and discount they get on a purchase of the product. But when there is numerous product with the special price may confused customer that how many percentages of discount they get on a purchase of the product? Instead of showing the percentage discount beside product price help to convert them easily and boost store conversion.
To display product discount percentage on product details page in Magento 2 using the following code.
To do the same, first we need to create ‘catalog_product_prices.xml’ at below location using following code.
app\code\Vendor\Extension\view\Base\layout\catalog_product_prices.xml
<pre class="lang:default decode:true"> <?xml version="1.0" encoding="UTF-8"?> <layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd"> <referenceBlock name="render.product.prices"> <arguments> <argument name="default" xsi:type="array"> <item name="prices" xsi:type="array"> <item name="final_price" xsi:type="array"> <item name="render_class" xsi:type="string">Magento\Catalog\Pricing\Render\FinalPriceBox</item> <item name="render_template" xsi:type="string">Vendor_ Extension::product/price/final_price.phtml</item> </item> </item> </argument> </arguments> </referenceBlock> </layout> </pre>
And create one more file to display price and discount percentage on product detail page.
app\code\Vendor\Extension\view\Base\templates\product\price\final_price.phtml
<pre class="lang:default decode:true"> <?php $priceModel = $block->getPriceType('regular_price'); $finalPriceModel = $block->getPriceType('final_price'); $idSuffix = $block->getIdSuffix() ? $block->getIdSuffix() : ''; $schema = ($block->getZone() == 'item_view') ? true : false; ?> <?php if ($block->hasSpecialPrice()): ?> <span class="special-price"> <?php echo $block->renderAmount($finalPriceModel->getAmount(), [ 'display_label' => __('Special Price'), 'price_id' => $block->getPriceId('product-price-' . $idSuffix), 'price_type' => 'finalPrice', 'include_container' => true, 'schema' => $schema ]); ?> </span> <span> <?php $item = $block->getSaleableItem(); $_savePercent = 100 - round(((float)$item->getFinalPrice() / (float)$item->getPrice()) * 100); echo '<b style="color:#008000">'.$_savePercent . '% off </b>'; ?> </span> <span class="old-price"> <?php echo $block->renderAmount($priceModel->getAmount(), [ 'display_label' => __('Regular Price'), 'price_id' => $block->getPriceId('old-price-' . $idSuffix), 'price_type' => 'oldPrice', 'include_container' => true, 'skip_adjustments' => true ]); ?> </span> <?php else: ?> <?php echo $block->renderAmount($finalPriceModel->getAmount(), [ 'price_id' => $block->getPriceId('product-price-' . $idSuffix), 'price_type' => 'finalPrice', 'include_container' => true, 'schema' => $schema ]); ?> <?php endif; ?> <?php if ($block->showMinimalPrice()): ?> <?php if ($block->getUseLinkForAsLowAs()):?> <a href="<?= $block->getSaleableItem()->getProductUrl() ?>" class="minimal-price-link"> <?= $block->renderAmountMinimal() ?> </a> <?php else:?> <span class="minimal-price-link"> <?= $block->renderAmountMinimal() ?> </span> <?php endif?> <?php endif; ?> </pre>
You are free to modify this code according to your business needs.
Lastly, hit that below stars if the code worked for you and don’t forget to comment down below if you are looking for any help regarding this code.
Happy Coding.
Hello
Thank you for this article.
Question, How can I show the precent discount for configurable products?
Thank you
We are going to write a separate blog for the configurable products soon. 🙂
Did this ever happen?
it doesn’t work in 2.2.5 magento version.
Are you facing any error or something for 2.2.5 version, if yes then please let us know. We will try to help you out.
How to display Save Amount on product page? use this same method?
Yes you can use the same method
any link for percent discount for configurable products.
No link as of now for this one honestly. You need to do the customization.
It’s not working in magento 2.4.5 version, any suggestions please
Confirm you create required file of the extension as well and extension is enabled.
What is the error you are facing, specify in some more details.
Hello,
We are attempting to set it up on Hyva theme however the discount percentage isn’t appearing on the PDP. On Luma it is showing up correctly. Seems like incompatibility issue with the latest version of the Hyva theme and module. Can you confirm this?
Hi Ameer
We have recently made this extension compatible with Hyva
Please check and download
https://gitlab.hyva.io/hyva-public/module-tracker/-/boards/7?search=magecomp
Hello
Thank you for this useful article.
I’ve query that how to show the precent discount for configurable products? as this is not working for configurable items.
Thank you
For the configurable product, further customization need to be done there.
For that you can contact us on support@magecomp.com