如何在Magento2中创建模块时覆盖模板文件


8

我想覆盖:

/opt/lampp/htdocs/magento_composer/vendor/magento/module-catalog/view/frontend/templates/product/view/addtocart.phtml

我已将其复制并粘贴到我的自定义模块中:

/opt/lampp/htdocs/magento_composer/app/code/Hello/Custom/view/frontend/templates/product/view/addtocart.phtml

这是我的布局文件:

<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>

        <referenceBlock name="product.info.addtocart" template="Hello_Custom::product/view/addtocart.phtml" after="alert.urls">

        </referenceBlock>

    </body>
</page>

在:

/opt/lampp/htdocs/magento_composer/app/code/Hello/Custom/view/frontend/layout/catalog_product_view.xml

module.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Hello_Custom" setup_version="1.0.0" schema_version="1.0.0">
    </module>
</config>
  • 模块适用于简单的hello world,因此其注册正确...

问题出在模板路径提示文件中,该文件来自供应商..但它应该仅来自我的自定义模块(而不是来自主题)


catalog_product_view.xml是必不可少的,否则我们可以给文件起任何名字。实际上,我需要有关覆盖模板的帮助
siddhesh

siddhesh该文件的命名绝对必要。它确定布局文件的加载位置。例如default.xml,在每个页面上加载,但catalog_product_view.xml仅在catalog/product/view操作上加载。
雅克

Answers:


17

您可以尝试使用xml文件中的以下代码,

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

是的,绝对是..
Narendra.vyas

如何检查是否使用布局xml文件?我试图编写无效的xml,但我的页面仍正确显示。所以我得出结论,我的xml布局文件没有执行,您告诉我如何解决吗
siddhesh

还是在更新xml文件后需要运行某种命令
siddhesh

1
实际上,在magento 2开发文档中,您可以阅读“使用<action>指令。不推荐这种方式,但是可以将其用于...”。我建议使用该页面上“ 设置块模板”下建议的方法。因此,使用操作来更改模板不是一个好的解决方案。
drew7721

10

Magento中有三种方法可以覆盖模板文件

例如:在这里,我们为简单产品覆盖了addtocart.phtml文件。

方法1:

<referenceContainer name="content">
    <referenceBlock name="product.info.addtocart">
        <action method="setTemplate">
            <argument name="template" xsi:type="string">Vendor_Module::product/view/addtocart.phtml</argument>
        </action>
    </referenceBlock>
</referenceContainer>

方法2:

<referenceContainer name="content">
    <referenceBlock name="product.info.addtocart" template="Vendor_Module::product/view/addtocart.phtml"/>
</referenceContainer>

方法3:

<referenceContainer name="product.info.form.content">
    <block class="Magento\Catalog\Block\Product\View" name="product.info.addtocart" as="addtocart" template="Vendor_Module::product/view/addtocart.phtml"/>
</referenceContainer>
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.