Search code examples
templatesmagentoblock

Magento - Inject custom module on product info page


I've searched on web for how to add "block" with my template on the product page. I build my custom module that displays some sort of information and I would like to show that, let's say under the long description of my product.

I've been trying to format the xml layout of my module but without luck.

After some search I've found this: Programatically create Magento blocks and inject them into layout which I can not make it work for me. But it is probably because I've missed something.

My module is structured like this:

CODE: app/code/local/deveti/Countrypurchase

DESIGN: app/design/frontend/default/default/template/Countrypurchase/index.phtml

LAYOUT: app/design/frontend/default/default/layout/countrypurchase.xml

I know the correct way is to edit main layout file, manually add a block, but I'd like to do that on the fly.

EDIT: this works!

So I would do in my module layout xml countrypurchase.xml something like this:

<?xml version="1.0"?>   
<layout version="1.0">
    <catalog_product_view>
        <reference name="product.info">
            <block type="core/template" name="product.countrypurchase" as="countrypurchase" template="countrypurchase/index.phtml" />
        </reference>
    </catalog_product_view>    
</layout>

And I've added a call to catalog/product/view.phtml:

<?php echo $this->getChildHtml('countrypurchase'); ?>

And it works ;)

Thank you for your help!


Solution

  • The problem with the product view page is that it's output is mainly controlled by the PHP code in the template catalog/product/view.phtml. Magento doesn't offer much extensibility points out of the box. You could add it to the content block but that would put your custom content either completely at the top or completely at the bottom.

    I think you are required to modify the template and add PHP code to render your custom block at the position you want, like:

    <?php
        echo $this->getChildHtml('product.countrypurchase');
    ?>
    

    With this in place you can either add your block with the name product.countrypurchase via the XML layout or programmatically.