2

What is the correct way to override a template file ? I need to display custom text in place of In Stock and Price on the product page.

enter image description here

I want to override the template -

Magento_Catalog/view/frontend/templates/product/view/type/default.phtml for In stock.

However, I cannot figure what would be the correct XML for it.

I've tried -

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
 <body>
 <referenceBlock name="product.info.type">
 <block class="Magento\Catalog\Block\Product\View\Type\Simple" name="product.info.simple" as="product_type_data" template="Vendor_Module::product/view/type/default.phtml" />
 </referenceBlock>
 </body>
</page>

I can't find out which file should be overridden to change the text for price.

I would really appreciate it if you could explain how to write the XML file for overriding templates.

UPDATED catalog_product_view.xml -

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
 <referenceContainer name="product.info.type">
 <block class="Magento\Catalog\Block\Product\View\Type\Simple" name="product.info.simple" as="product_type_data" template="Vendor_Module::product/view/type/default.phtml"/>
 </referenceContainer>
</body>
</page>

Vendor/Module/view/templates/product/view/type/default.phtml -

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
// @codingStandardsIgnoreFile
?>
<?php /* @var $block \Magento\Catalog\Block\Product\View\AbstractView */?>
<?php $_product = $block->getProduct() ?>
<?php if ($block->displayProductStockStatus()): ?>
 <?php if ($_product->isAvailable()): ?>
 <div class="stock available" title="<?= /* @escapeNotVerified */ __('Availability') ?>">
 <span><?= /* @escapeNotVerified */ __('In stock with static text') ?></span>
 </div>
 <?php else: ?>
 <div class="stock unavailable" title="<?= /* @escapeNotVerified */ __('Availability') ?>">
 <span><?= /* @escapeNotVerified */ __('Out of stock') ?></span>
 </div>
 <?php endif; ?>
<?php endif; ?>
asked Mar 29, 2019 at 21:05

2 Answers 2

0

If want to override below file using module:

Magento_Catalog/view/frontend/templates/product/view/type/default.phtml

Create

app/code/{Vendor}/{Module}/view/frontend/layout/catalog_product_view.xml

and put

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
 <referenceContainer name="product.info.type">
 <block class="Magento\Catalog\Block\Product\View\Type\Simple" name="product.info.simple" as="product_type_data" template="{Vendor}_{Module}::product/view/type/default.phtml"/>
 </referenceContainer>
</body>
</page>

Now create default.phtml at:

app/code/{Vendor}/{Module}/view/frontend/templates/product/view/type/default.phtml

Now you can add you code in phtml.

Hope above will Help!

answered Mar 30, 2019 at 2:18
14
  • Thanks for this. Can you please explain how to create the xml file. How do you find which container and block to use? Do the attributes name and as have to be same as in Magento_catalog's catalog_product_view.xml? Commented Mar 31, 2019 at 1:07
  • Which files should I edit to change the Price? Commented Mar 31, 2019 at 1:08
  • First I check file name using template path hint. after find file name we can search that file in xml. we get reference block/container name from here. Commented Mar 31, 2019 at 1:48
  • you can see for override I have used same reference container, block class, name etc. I have only change phtml with our module! Commented Mar 31, 2019 at 1:51
  • 1
    it doesn't work Commented Apr 1, 2019 at 13:43
0

create layout file in your custom module as below path:

{vendor}/{Module}/view/frontend/layout/catalog_product_view_type_simple.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
 <body>
 <referenceContainer name="product.info.type">
 <referenceBlock name="product.info.simple">
 <action method="setTemplate">
 <argument name="template" xsi:type="string">Vendor_Module::product/view/type/default.phtml</argument>
 </action>
 </referenceBlock>
 </referenceContainer>
 </body>
</page>

For example: Here we override product/view/type/default.phtml in custom module.

answered Feb 17, 2020 at 13:30
1
  • I tried it, but it does not work. I tried to override the template \vendor\magento\module-checkout\view\frontend\templates\cart\item\renderer\actions\edit.phtml in my module. Commented Aug 7, 2020 at 8:26

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.