Add some block to product view page through module xml file Magento

Add some block to product view page through module xml file Magento

July 3, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Add some block to product view page through module xml file Magento

Adding a Block to the Product View Page via XML in Magento

Hello developers! Today we’re going to dive into how you can add a block to your product view page using an XML layout file in Magento. This is a common requirement when building custom extensions, and it’s a straightforward process once you understand the syntax.

Current XML Layout File

Here’s the current XML layout file provided by the user:

<layout version="0.1.0">
    <total_index_index>
        <reference name="root">
            <action method="setTemplate"><template>page/2columns-right.phtml</template></action>
        </reference>

        <reference name="content">
            <block type="total/prototal"  name="total_prototal" template="total.phtml" />
        </reference>
    </total_index_index>
</layout>

This layout is currently set up to work on the index action (presumably a custom controller). The content of total.phtml is being displayed within the main content area (<content></content>).

Adding the Block to the Product View Page

To add this block to the product view page, we need to modify the layout XML file. Here’s how you can do it:

  1. Open your custom module’s app/code/Vendor/Module/view/frontend/layout folder.

  2. Inside this folder, create a new XML file named catalog_product_view.xml. This is the standard file name for layouts that apply to the product view page.

  3. Add the following lines inside the <layout> tags:

<referenceBlock name="content">
    <block class="Magento\Framework\View\Element\Template" template="Vendor_Module::total.phtml"/>
</referenceBlock>

Let’s break down this code:

  • name="content" refers to the main content area of the page.

  • <block> declares a new block element.

  • The class attribute specifies that we’re using Magento’s built-in Template block class for rendering our custom template. This is a good practice as it keeps things clean and separate from your module’s specific business logic.

  • template="Vendor_Module::total.phtml" sets the path to your custom total.phtml file relative to your module’s view directory.

Conclusion

And that’s how you add a block to the product view page using an XML layout file in Magento! Remember, the key is to create a new catalog_product_view.xml file and use the correct <referenceBlock> syntax within it.

Don’t forget to clear your cache after making changes to ensure they take effect. You can do this via the command line with php bin/magento cache:clean or through the Magento admin interface under System > Cache Management.

For more advanced layout customizations, consider reading our comprehensive guide on Working with Layout XML in Magento.

Happy coding!

Catalog Extensions Themes Pricing

Our network