The question:
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.
The Solutions:
Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.
Method 1
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>
Method 2
Suppose our Module Name ‘CustomSales’ and Package Name “Exercise”.
Step 1 : Create a module config file into app/etc/modules/Exercise_CustomSales.xml
<config>
<modules>
<Exercise_CustomSales>
<active>true</active>
<codePool>local</codePool>
</Exercise_CustomSales>
</modules>
Step 2: Create a config file under app/code/local/Exercise/CustomSales/etc/config.xml
<config>
<modules>
<Exercise_CustomSales>
<version>1.7.0.2</version>
</Exercise_CustomSales>
</modules>
<global>
<blocks>
<customsales>
<class>Exercise_CustomSales_Block</class>
</customsales>
<sales>
<rewrite>
<order_view>Exercise_CustomSales_Block_Sales_Order_View</order_view>
</rewrite>
</sales>
</blocks>
</global>
Step 3 : Create a php file which is overwrite the actual view.phtml file, app/code/local/Exercise/CustomSales/Block/Sales/Order/View.php
class Exercise_CustomSales_Block_Sales_Order_View extends Mage_Sales_Block_Order_View { protected function _construct() { parent::_construct(); $this->setTemplate('customsales/sales/order/view.phtml'); } }
Step 4 : you should create a local.xml file inside layout folder(Possible path: app/design/frontend/package/theme/layout). This is for print.phtml .
e.g (app/design/frontend/rwd/default/layout/local.xml)
<layout>
<sales_order_print>
<reference name="sales.order.print">
<action method="setTemplate">
<template>customsales/sales/order/print.phtml</template>
</action>
</reference>
</sales_order_print>
Move view.phtml and print.phtml file inside “app/design/frontend/package/theme/template/customsales/sales/order”.
Clear magento cache.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0