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

appdesignfrontendbasedefaulttemplatecatalogproductview.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?

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

    • 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.

  1. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *