Magento Module Template – How to Overwrite Core Template Files from a Custom Module

magento-1moduleoverridessales-ordertemplate

I want to overwrite two files. Namely the view.phtml and print.phtml of sales orders.
(path: app/design/frontend/base/default/template/sales/order/)

I am creating a module in which I want to create a path as app/design/frontend/base/default/template/<My Module Name>/sales/order/ so that the core view.phtml and print.phtml files don't get overwritten.

So please guide me how to achieve this task.

Best Answer

You could do this with a layout XML file inside of your module. You need to have a section in your module's config.xml like this to let Magento load your module's layout XML (path: app/design/frontend/base/default/layout/mymodulename.xml):

<config>
    [...]
    <frontend>
        [...]
        <layout>
            <updates>
                <mymodulename_layout module="MyModuleName">
                    <file>mymodulename.xml</file>
                </mymodulename_layout>
            </updates>
        </layout>
    </frontend>

In this layout XML file you can reference the specific blocks and change their template.

<layout>
    <sales_order_view>
        <reference name="sales.order.view">
            <action method="setTemplate">
                <template>mymodulename/sales/order/view.phtml</template>
            </action>
        </reference>
    </sales_order_view>
    <sales_order_print>
        <reference name="sales.order.print">
            <action method="setTemplate">
                <template>mymodulename/sales/order/print.phtml</template>
            </action>
        </reference>
    </sales_order_print>
</layout>