Magento Event Observer – Add Block with Observer Event

blocksevent-observerextensions

I want to add a block inside another block I have this structure:

enter image description here

in config.xml for the observer part I have this:

......
 <frontend>
       <events>
            <core_block_abstract_to_html_before>
                <observers>
                    <cart_shopping>
                        <type>model</type>
                        <class>cart_shopping/observer</class>
                        <method>insertBlock</method>
                    </cart_shopping>
                </observers>
            </core_block_abstract_to_html_before>
        </events>
....

and in observer I search for block and then I create the new block:

class Cart_Shopping_Model_Observer
{

        public function insertBlock($observer)
        {
            /** @var $_block Mage_Core_Block_Abstract */
            /*Get block instance*/
            $_block = $observer->getBlock();
            /*get Block type*/
            $_type = $_block->getType();
           /*Check block type*/
            if ($_type == 'catalog/product_price') {
                /*Clone block instance*/
                $_child = clone $_block;
                /*set another type for block*/
                $_child->setType('test/block');
                /*set child for block*/
                $_block->setChild('child', $_child);
                /*set our template*/
                $_block->setTemplate('at.phtml');
            }
        }
    }

my theme is rwd so:
I have put at.phtml in app/design/frontend/rwd/default/template/catalog/product/at.phtml

and here is at.phtml:

<?php echo $this->getChildHtml('child') ?>
<h1>Hello</h1>

It is not adding the block, why? do I need any custom layout file or changing any local.xml file?! I don't know what else I should do
I tried to do observer solution with this link

Best Answer

I understood my problem it was in config.xml, I changed class like this:

 <core_block_abstract_to_html_before>
                <observers>
                    <cart_shopping>
                        <type>model</type>
                        <class>Cart_Shopping_Model_Observer</class>
                        <method>insertBlock</method>
                    </cart_shopping>
                </observers>
            </core_block_abstract_to_html_before>

and also I did what Amit said about the template

Related Topic