I am writing a custom module for magento 2 i.e "call for price" and I want to override price html on product list page.

I have follow the following scenario:


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <preference for="Magento\Catalog\Pricing\Render\FinalPriceBox" type="Mymodule\Callforprice\Block\Callforprice" />



namespace Mymodule\Callforprice\Block;
 * Product price block
class Callforprice extends \Magento\Catalog\Pricing\Render\FinalPriceBox 

     * Wrap with standard required container
     * @param string $html
     * @return string
    protected function wrapResult($html)
        if($this->getSaleableItem()->getCallforprice()): // getCallforprice() is product attribute
            return '<div class="price-box "><span>Call For Price</span></div>';
        else :
            return '<div class="price-box ' . $this->getData('css_classes') . '" ' .
                'data-role="priceBox" ' .
                'data-product-id="' . $this->getSaleableItem()->getId() . '"' .
                '>' . $html . '</div>';


Any suggestion would be appreciated.



Here price hide when guest user login and price show after customer login.



<?xml version="1.0" encoding="UTF-8"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Pricing\Render\FinalPriceBox">
        <plugin name="price_hide" type="Vendorename\Modulename\Plugin\HidePriceBox" sortOrder="1" disabled="false"/>




namespace Vendorename\Modulename\Plugin;

class HidePriceBox
    protected $_customerSession;

    public function __construct(
        \Magento\Customer\Model\Session $session
    ) {
        $this->_customerSession = $session;

    function afterToHtml(\Magento\Catalog\Pricing\Render\FinalPriceBox $subject, $result)
        if ($this->_customerSession->isLoggedIn()) {
            return $result;
        } else {
            return '';

In Vendor/Module/view/base/layout/catalog_product_prices.xml do something like:

<?xml version="1.0"?>
<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">
        <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_Module::product/price/simple/final_price.phtml</item>
        <argument name="configurable" xsi:type="array">
            <item name="prices" xsi:type="array">
                <item name="final_price" xsi:type="array">
                    <item name="render_class" xsi:type="string">Magento\ConfigurableProduct\Pricing\Render\FinalPriceBox</item>
                    <item name="render_template" xsi:type="string">Vendor_Module::product/price/configurable/final_price.phtml</item>

As you can see we are overriding 2 templates, for simple and configurable product.

Then add .phtml files in paths configured in xml.

This snippet actually overrides price blocks' templates on product page but you can put it in appropriate layout file.

  • You should definitely NOT be modifying anything within Vendor files. – Josh Undefined Jan 12 '18 at 9:37
  • 6
    Dude, it is a placeholder for his vendor name..... you serious? – Bartosz Herba Jan 12 '18 at 10:01
  • Did anyone try this solution, is it working? – Mohit Rane Mar 12 '20 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.