9

I am creating a module and want to overwrite the template app/code/Magento/Catalog/view/adminhtml/templates/catalog/product/edit/options.phtml.

Is this possible in a module (not a theme)

Unfortunately it does not seem super-easy to change the module name via layout XML, as this is set hard-coded in https://github.com/magento/magento2/blob/develop/app/code/Magento/Catalog/Block/Adminhtml/Product/Edit/Tab/Options.php#L21

10

Add layout folder of you module file [Vendor]/[ModuleName]/view/adminhtml/layout/catalog_product_options.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="admin.product.options">
        <arguments>
            <argument name="template" xsd:type="string">[Vendor]/[ModuleName]::product_options.phtml</argument>
        </arguments>
    </referenceBlock>
</layout>

Also need add to module xml this line, to load you layouts after catalog

<sequence>
        <module name="Magento_Catalog"/>
</sequence>
  • Where did you get the name admin.product.options from? – Alex Nov 3 '15 at 9:23
  • Just search \Magento\Catalog\Block\Adminhtml\Product\Edit\Tab\Options by layout files and find out declaration in Magento/Catalog/view/adminhtml/layout/catalog_product_options.xml – KAndy Nov 3 '15 at 9:46
  • 1
    Shouldn't it be Vendor_ModuleName::product_options.phtml? – Giel Berkers Jun 28 '16 at 12:14
  • Yes, thank for point – KAndy Jun 28 '16 at 16:06
  • 2
    This solution only works, if the original template was set in the Block.php file. If the original template is set on the layout.xml, this solution will not work. A better approach is to always use <action method="setTemplate"> instead of <arguments>. This solution works in all cases. – Jalogut Feb 10 '17 at 8:56
3
  1. Create corresponding layout file in your module:

[Vendor]/[ModuleName]/view/adminhtml/layout/catalog_product_options.xml

  1. Set your custom template configuration
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
    <referenceBlock name="admin.product.options">
        <action method="setTemplate">
            <argument name="template" xsi:type="string">[Vendor]_[ModuleName]::product_options.phtml</argument>
        </action>
    </referenceBlock>
</layout>

NOTE: it is very important to use <action method="setTemplate"> instead of <arguments>. <arguments> will only work if the block does not have another template specified on the xml declaration. In any other case, you need <action method="setTemplate"> to override the existing template. It is better to use <action method="setTemplate"> because it always works.

0

Yes, you can overwrite template files from app/code of your custom module. However, do keep in mind that in Magento's inheritance concept, files (templates, layouts, static files) from app/design overwrite the same in app/code. Just as child theme overwrites parent theme.

So, I consider it best to create a custom theme for your custom module and add all your phtml, xml, css, js, font and image files there in app/design.

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.