Magento 1.9.0.1 – How to Add a Block to an Existing Tab Programmatically

blocksce-1.9.0.1

I'm trying to add a block to an existing tab on the product view page in Magento, programmatically.

The tab is the pre-existing description tab, around line 185 of the catalog xml file at app/design/frontend/base/default/layout/catalog.xml

<block type="catalog/product_view_description" name="product.description" as="description" template="catalog/product/view/description.phtml">
    <action method="addToParentGroup"><group>detailed_info</group></action>
    <action method="setTitle" translate="value"><value>Description</value></action>
</block>

I'm using a custom module and trying to inject my block after the description content, in the same tab. Here's the code:

class My_Details_Block_Product extends Mage_Core_Block_Template
{
    protected $_template = 'details/product_details.phtml';

    protected function _prepareLayout() {
        if ($addToTabBlock = $this->getLayout()->getBlock('product.description')) {
            $addToTabBlock->insert($this, '', false, 'details');
        }
        return $this;
    }
}

There are no errors available in any of the logs, but the block simply doesn't appear. I've also tried to use the append method:

$addToTabBlock->append($this, 'other');

But get the same (blank) result.

How can I insert a block to the existing tab? Thanks.

Best Answer

You can try this. I didn't test this code.

class My_Details_Block_Product extends Mage_Core_Block_Template
{
    protected $_template = 'details/product_details.phtml';

    protected function _prepareLayout() {
        if ($addToTabBlock = $this->getLayout()->getBlock('product.description')) {

            //sets this block as child to description block
            $addToTabBlock->setChild( 'my.custom.block',$this->getLayout()->createBlock( $this,'my.custom.block',array('template' => 'details/product_details.phtml')));
        }
        return parent::_prepareLayout();
    }
}

You may need to call this block in description.phtml as like this

<div><?php echo $this->getChildHtml('my.custom.block');  ?> </div>

in the desired position