Home > OS >  Magento 2 Override price in catalog view of a configurable product
Magento 2 Override price in catalog view of a configurable product

Time:10-11

I have Magento 2.

I write a module.

I write this file: app\code\Vendor\MyModule\etc\di.xml

<?xml version="1.0"?>
<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="Vendor\MyModule\Block\Callforprice" />
</config>

I write this class: app\code\Vendor\MyModule\Block\Callforprice.php

namespace Vendor\MyModule\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)
    {

        return '-TEST-<div  ' .
            'data-role="priceBox" ' .
            'data-product-id="' . $this->getSaleableItem()->getId() . '" ' .
            'data-price-box="product-id-' . $this->getSaleableItem()->getId() . '"' .
            '>' . $html . '</div>';
    }

}

This code works fine with simple product in catalog view but not with configurable product. How can i override html of price of a configurable product in catalog view?

CodePudding user response:

I found the solution. I have to override:

Magento\ConfigurableProduct\Pricing\Render\FinalPriceBox

Also i put the template in:

app\design\frontend\Vendor\myTheme\Magento_ConfigurableProduct\templates\product\price\final_price.phtml
  • Related