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

2

3 Answers 3

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>
5
  • Where did you get the name admin.product.options from?
    – Alex
    Commented Nov 3, 2015 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
    Commented Nov 3, 2015 at 9:46
  • 2
    Shouldn't it be Vendor_ModuleName::product_options.phtml? Commented Jun 28, 2016 at 12:14
  • Yes, thank for point
    – KAndy
    Commented Jun 28, 2016 at 16:06
  • 3
    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
    Commented Feb 10, 2017 at 8:56
4
  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 and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.