0

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.

2
  • are you sure, you block instantiates when page loads. put die(); inside your preapareLayout() and give me the result Aug 21, 2014 at 13:45
  • @programmer_rkt yes, it instantiates. die(); produces a blank page. Also, var_dump('anything'); inside the if(..) loop spits out on the page. Thx.
    – Jongosi
    Aug 21, 2014 at 13:47

2 Answers 2

1

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

8
  • I did need to call the block in the phtml file. Thx for the help
    – Jongosi
    Aug 21, 2014 at 14:58
  • what about the _prepareLayout() method ? Your code worked? Aug 21, 2014 at 15:08
  • did you try my code? Is that worked ? Aug 21, 2014 at 15:18
  • Yes, your code worked, thx.
    – Jongosi
    Aug 21, 2014 at 15:18
  • I would like to know, how did you make trigger your block when product view page loads? Aug 21, 2014 at 15:19
1

You could do that or you could try another way. Add a layout update xml file and in that xml file add a layout update:

<reference name="description">
   <block type="core/template" name="a_nice_name" template="some/path/to/details/product_details.phtml" />
</reference>
4
  • Thanks @Samuel, incidentally that doesn't work for tabs (or for me!). Also, your block is missing a self-closing tag />.
    – Jongosi
    Aug 21, 2014 at 13:59
  • 1
    It surely works. Magento is just stubborn if something is slightly wrong. Anyway, enable template path hints and see what blocks are loaded in that specific area. Also, check if your module is loaded and also if your xml is loaded. If all is ok by now, it's just an xml error. For that, you can check your var/log. Aug 21, 2014 at 14:03
  • 1
    @SamuelComan : You idea is correct. But he needs to add his own block rather that core/template. But he can addopt this method to add his block. Also he need to call his block in description.phtml also Aug 21, 2014 at 14:20
  • Thanks @Samuel, I believe you, but still doesn't work for tabs on my stack; maybe a theme issue. BTW, your reference tag should be <reference name="description">, for future visitors.
    – Jongosi
    Aug 21, 2014 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.