How to add custom block into product view page?

This is a simple problem but it is popular for Magento developers. Sometimes, we need to add a custom block into product view page without editing core files. For example, you might want to show a contact form in product page. Well, as I said, it is not really difficult and in this post, I will show you how to do it by 2 methods which I often use.

Solution 1

Normally, you can usereference name of product page in layout file catalog.xml – product.info

And in the layout file of your custom module, put this piece of code:

<catalog_product_view>

<reference name="product.info">

<block type="your_module/your_block" as="moduleAs" template="moduletemplate/ moduletemplate.phtml" />

</reference>

</catalog_product_view>

And then in template file view.phtml app/design/frontend/your_package/your_theme/template/catalog/product/view.phtml,add the following code:

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

Solution 2

- First, in layout file of your module, you add the following code

<catalog_product_view>

<reference name="product.info.extrahint">

<block type="your_module/your_block" as="moduleAs" template="moduletemplate/ moduletemplate.phtml" />

</reference>

</catalog_product_view>

You will see content you want to show will display after price box

- If you want to add a form under product detail section, add the following code in xml file:

<catalog_product_view>

<reference>

<block as="moduleAs" template="moduletemplate/ moduletemplate.phtml ">

<actionmethod="addToParentGroup"><group>detailed_info</group></action>

</block>

</ reference>

</catalog_product_view>

The content will display in group“detail info” of product page

Well, you see, it is not tricky and I hope that it can help you develop a module yourself. Enjoy coding!!!

One thought on “How to add custom block into product view page?”

1 Response to How to add custom block into product view page?

Comments