2

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

2

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

| improve this answer | |
2

Your template path location is wrong:

It should be $_block->setTemplate('catalog/product/at.phtml'); as you define template location

app/design/frontend/rwd/default/template/catalog/product/at.phtml

As magento every template file location is define at layout and php code after template/.

Note: you must need to use RWD Package

As you have using cart_shopping/observe that means you have used xml pattern for call observer class that why need define Model class prefix in xml

  <global>
        <models>
            <cart_shopping><!-- model class prefix -->
                <class>Cart_Shopping_Model</class>
            </cart_shopping>
        </models>
    </global>
| improve this answer | |
  • I don't see hello yet – Nickool Apr 15 '15 at 18:49
  • should I write any code in my layout? – Nickool Apr 15 '15 at 18:49
  • I +1 your answer but it did the part of problem – Nickool Apr 15 '15 at 19:09
1

The other option is to add the model definition to your config.xml

<global>
    <models>
        <cart_shopping>
            <class>Cart_Shopping_Model</class>
        </cart_shopping>
    </models>
</global>

This will mean that the observer can use the config cart_shopping/observer or as you have commented you can switch to the full path Cart_Shopping_Model_Observer

| improve this answer | |
  • thank you so much but I'm curious why cart_shopping/observer did not work and <class>Cart_Shopping_Model_Observer</class> worked – Nickool Apr 15 '15 at 19:59
  • because you where missing the definition that I have started but Magento can work out the model when provided with a full path even without the definition. – David Manners Apr 15 '15 at 20:00
  • I got it finally, yes I did not have that one – Nickool Apr 15 '15 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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