Magento - Inject custom module on product info page

Magento - Inject custom module on product info page

October 4, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - Inject custom module on product info page

Injecting Custom Blocks into Magento Product Pages

In Magento, adding custom blocks to product pages is a common requirement for extending functionality or displaying additional information. In this post, we’ll guide you through the process of injecting a custom block with your module’s template under the product description.

Understanding Magento Layouts and XML

Magento uses an XML-based layout system to define the structure of its pages. The catalog_product_view layout handle is responsible for rendering the product page. To add a new block, we need to modify this layout file programmatically or via a custom module’s layout XML.

Here’s what you’ll need:

  • A custom module directory: app/code/local/deveti/Countrypurchase
  • The template file for your block: app/design/frontend/default/default/template/countrypurchase/index.phtml
  • The layout XML for your module: app/design/frontend/default/default/layout/countrypurchase.xml

Writing the Layout XML

Open your custom layout XML file and add a new reference to the product.info block, which contains all the main content of the product page. Inside this reference, create a new block with the core/template type.

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

In this example:

  • name is a unique identifier for the block.
  • as is the alias used to reference the block in other parts of the layout or view files.
  • template points to the location of your custom template file.

Calling the Block in Your View File

Next, you need to call this new block from within the product view file where you want it to appear. Open catalog/product/view.phtml and add the following line after the long description section:

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

The getChildHtml() method retrieves and renders all the blocks associated with the current layout handle (catalog_product_view) for the provided alias (in this case, ‘countrypurchase’).

Conclusion

By following these steps, you can easily inject a custom block into the Magento product page using your module’s layout XML. This approach allows you to add dynamic content or functionality without manually modifying the core layout files.

Remember that in a production environment, it’s best practice to create a child theme and place your layout changes there to avoid conflicts during upgrades. Always make sure to clear your cache after making changes to Magento layouts.

For more advanced techniques on programmatically creating blocks and injecting them into layouts, you can refer to this article on Inchoo.

Happy coding!

Catalog Extensions Themes

Our network