override product/price/msrp.phtml template
-
07-10-2020 - |
Frage
In my module I created \view\frontend\layout\catalog_product_prices.xml
<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="msrp_price" xsi:type="array">
<item name="render_class" xsi:type="string">Magento\Catalog\Pricing\Render\PriceBox</item>
<item name="render_template" xsi:type="string">MyVendor_MyModule::product/price/msrp.phtml</item>
</item>
</item>
</argument>
</arguments>
</referenceBlock>
</layout>
but my custom product/price/msrp.phtml template did not override core template. What am i doing wrong?
Lösung
Let's assume that the name of your module is Yourvendor_Yourmodule.
- Create
app/code/Yourvendor/Yourmodule
directory Create file
app/code/Yourvendor/Yourmodule/registration.php
:\Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Yourvendor_Yourmodule', __DIR__ );
Create file
app/code/Yourvendor/Yourmodule/etc/module.xml
:<?xml version="1.0" ?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Yourvendor_Yourmodule" setup_version="1.0.0"> <sequence> <module name="Magento_Catalog"/> <module name="Magento_Msrp"/> </sequence> </module> </config>
Create file
app/code/Yourvendor/Yourmodule/view/base/layout/catalog_product_prices.xml
:<?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="msrp_price" xsi:type="array"> <item name="render_class" xsi:type="string">Magento\Catalog\Pricing\Render\PriceBox</item> <item name="render_template" xsi:type="string">Yourvendor_Yourmodule::product/price/msrp.phtml</item> </item> </item> </argument> </arguments> </referenceBlock> </layout>
Create file
app/code/Yourvendor/Yourmodule/view/base/templates/product/price/msrp.phtml
and put some content in order to be sure that it's used on frontend.
Perform following commands:
php bin/magento cache:clean
php bin/magento setup:upgrade
Depending on your environment setup and mode, you'll have to execute
php bin/magento setup:di:compile
Go to the page with msrp set, and check if it works.
If it still doesn't work
Check \Magento\Framework\Pricing\Render\RendererPool
. If you have installed debug, you can observer findDataByPattern()
function in order to see what renderer is getting assigned. If you don't have xdebug, then just put $renderers = $this->getData(); var_dump($renderers['default']['prices']); die();
somewhere in findDataPattern()
and check what templates are used for rendering different type of prices.