Magento 1.9 – How to Insert Custom Template/Block

blocksmagento-1.9template

Trying to insert custom template/block into specific place in => product.info.addto

for some reason my block appears either before or after and I can't manage to insert it anywhere else.

<?xml version="1.0" encoding="UTF-8"?>
<layout>
<catalog_product_view>

    <reference name="content">

        <block type="amber_custom/catalog_product_custom" name="linked.product" template="amber/catalog/product/custom.phtml" after="product.reviews"/> <!-- trying inset it after this block but no luck -->

    </reference>

</catalog_product_view>
</layout>

What is the problem, why it won't go there ?

To find out all the block names i'm using "ath" – advanced theme hint , it shows all the names correctly.

Best Answer

Changes

  1. Copy catalog/product/view.phtml into to your theme if it isn't already (never directly edit core files or you could lose your changes)
  2. Inside this template add <?php echo $this->getChildHtml('linked.product') ?> to where you want it to render
  3. In your layout XML change <reference name="content"> to <reference name="product.info">

Explanation

The problem arises because you're using <reference name="content"> - content is a structural block and structural blocks will automatically render child blocks and this is your problem as it can be difficult to position blocks this way.

Why before/after isn't working for you

after isn't working for you because your block is using the reference content whilst product.reviews inside the reference of product.info.

You're best bet is to use <reference name="product.info"> rather than content and then copy over catalog/product/view.phtml into your theme if it isn't already (never edit the core or Ben Marks will hunt you down).

Now inside this template add the following:

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

This will render your block where ever you add that line of code.

At first the above seems a bit strange and not so straight forward, but after you've done it a few times it becomes second nature and actually makes sense.

Related Topic