2

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:

1.etc/di.xml

<?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" />
</config>

2.Block/Callforprice.php

<?php

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>';
        endif;
    }

}

Any suggestion would be appreciated.

Thanks

0

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">
    <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_Module::product/price/simple/final_price.phtml</item>
                </item>
            </item>
        </argument>
        <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>
                </item>
            </item>
        </argument>
    </arguments>
</referenceBlock>

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

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.