10

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.

13

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>
  • 2
    Perfect!!! Worked like a Hot cake. Thanks. :) – Rajan Faldu Mar 27 '15 at 9:42
  • Your welcome. I like hot cake :P – 7ochem Mar 27 '15 at 9:44
  • @7ochem, how to achieve same in magento2? – prasad maganti Oct 15 '15 at 11:16
  • You should ask this as a new question as it is too broad to put it in a comment here and is not what the OP is asking – 7ochem Oct 15 '15 at 12:56
3

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.

  • There's no need to rewrite the whole block. In my answer the constructor of Mage_Sales_Block_Order_View get's called first and sets the template, then setTemplate get's called initiated by the layout XML from my example and sets the custom template. I'd like to go for solutions that don't use rewrites as they can potentially cause conflicts. – 7ochem Mar 27 '15 at 8:59
  • 7ochem is right. No need of setting the Template in Block. 7ochem's solution is good and it worked for me. – Rajan Faldu Mar 27 '15 at 9:45
  • It's depends upon you. If you overwrite or add some php method then you need to overwrite block otherwise you just change the template through module.xml or local.xml . My solution doesn't effect any magento version, I guess. – Sohel Rana Mar 27 '15 at 10:38

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.