PraveenMax
PraveenMax

Reputation: 717

In Magento ,is it possible to modify the default template files via custom modules without hacking into its *.phtml core files?

I am creating a custom module where i added a button in product view page(default file).I have inserted the button code in the following file

app\design\frontend\base\default\template\catalog\product\view.phtml

and the button gets displayed & works well.But everytime Magento is updated , the above file gets replaced and so my code is discarded.Is there any way to modify or extend or inject the template coding from within my custom module?Should i need to override the core blocks or somehting?

Upvotes: 1

Views: 3108

Answers (3)

clockworkgeek
clockworkgeek

Reputation: 37700

  • Give your module a layout file:

    ...
    <frontend>
        <layout>
            <updates>
                <YOUR_MODULE>
                    <file>YOURMODULE.xml</file>
                </YOUR_MODULE>
            </updates>
        </layout>
    </frontend>
    ...
    
  • In the base theme's layout/YOURMODULE.xml file:

    <layout version="1.0">
        <catalog_product_view>
            <reference name="product.info">
                <action method="setTemplate"><name>YOURMODULE/product/view.phtml</name></action>
            </reference>
        </catalog_product_view>
    </layout>
    
  • Copy the modified view.phtml to a new directory, template/YOURMODULE/product/

  • When packaging your module remember to include all these files but don't overwrite template/catalog/product/view.phtml.

Anyone downloading your module might want to make their own changes to that template so you should also include instructions on which file has been changed and where to find it. Since view.phtml is often changed by both themers and other modules it would be a much better idea to alter one of the child templates instead - which one depends on where your button will be.

As a bonus there is a block called extrahint just after the prices of type core/text_list - it doesn't have a template so you might normally miss it when using template path hints. You can safely add as many templates as you like to it without altering any existing ones:

<layout version="1.0">
    <catalog_product_view>
        <reference name="product.info.extrahint">
            <block type="core/template" name="YOUR.CUSTOM.BLOCK" template="YOUR/CUSTOM/TEMPLATE.phtml" />
        </reference>
    </catalog_product_view>
</layout>

That only helps if just after the price is useful for you. Other blocks that can be safely appended are the options and containers areas, but they only show for products with options.

Upvotes: 8

Anton S
Anton S

Reputation: 12750

You can't build your custom extension to be dependent on default views it shows bad planning. you should either clone this template or extend it and have ways to enable and disable your functionality. The must have thing is "designers guide" if your module implements templates or has ways to modify other templates.

Upvotes: 1

David Stinemetze
David Stinemetze

Reputation: 9320

The problem is that you are installing it in app/design/frontend/base/default.

You need to create a copy of this directory in another directory, such as app/design/frontend/default/yoursite.

Read this for more info on how to do it specifically

Upvotes: 1

Related Questions