Back to Top

Add custom text under price details on product page Magento 2

Updated 4 January 2023

Hello guys, too often we need to add some custom text under the price details on the product view page. Here is how we can do it without many efforts.

The first step is to overwrite the pricing renderer-

for this, you can add the following code in di.xml in the etc folder-

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Catalog\Pricing\Render\FinalPriceBox" type="Webkul\CustomModule\Pricing\Renderer\FinalPriceBox"/>
</config>

The second step is to create the renderer file that we have defined in di.xml

write the following code to FinalPriceBox.php in Webkul\CustomModule\Pricing\Renderer Folder –

Searching for an experienced
Magento Company ?
Find out More
<?php
namespace Webkul\CustomModule\Pricing\Renderer;

use Magento\Catalog\Pricing\Price;
use Magento\Framework\Pricing\Render\PriceBox as BasePriceBox;
use Magento\Msrp\Pricing\Price\MsrpPrice;
use Magento\Catalog\Model\Product\Pricing\Renderer\SalableResolverInterface;
use Magento\Framework\View\Element\Template\Context;
use Magento\Framework\Pricing\SaleableInterface;
use Magento\Framework\Pricing\Price\PriceInterface;
use Magento\Framework\Pricing\Render\RendererPool;
use Magento\Framework\App\ObjectManager;
use Magento\Catalog\Pricing\Price\MinimalPriceCalculatorInterface;

class FinalPriceBox extends \Magento\Catalog\Pricing\Render\FinalPriceBox
{
    
    public function __construct(
        Context $context,
        SaleableInterface $saleableItem,
        PriceInterface $price,
        RendererPool $rendererPool,
        \Magento\Framework\Registry $registry,
        array $data = [],
        SalableResolverInterface $salableResolver = null,
        MinimalPriceCalculatorInterface $minimalPriceCalculator = null
    ) {
        parent::__construct($context, $saleableItem, $price, $rendererPool, $data);
        $this->_registry = $registry;
    }
    
    public function wrapResult($html)
    {
        return '<div class="price-box '.$this->getData('css_classes').'" '.'data-role="priceBox" '.'data-product-id="'.$this->getSaleableItem()->getId().'"'.'>'.$html.'(Price/Kilometer)</div>';
    }
}
additional text

That is all. Now save the files, flush the cache and see the magic on the Product view page.
Here is how it will look like-

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home