First we need to create ‘catalog_product_prices.xml’ at below location using the given code.
app\code\Magemonkeys\Discountpercentage\view\base\layout\catalog_product_prices.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?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">Magemonkeys_Discountpercentage::product/price/final_price.phtml</item> </item> </item> </argument> </arguments> </referenceBlock> </layout> |
And create one more file to display the price and discount percentage on the product detail page.
app\code\Magemonkeys\Discountpercentage\view\base\templates\product\price\final_price.phtml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
<?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; ?> |
Output :
[crayon-63e0a6e332a5c585840211/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.