2

The goal is to append a block, after the product price, on the product view page using an observer.

To this moment, I was able to:

  • clone the catalog/product_price
  • set the clone as a child-block
  • and set a custom type and template for it

The issue is, that there are additional blocks on the page, that include a catalog/product_price block. Tier-prices and related products namely. I would like exclude them from the block cloning and child addition process.

the relevant config.xml part:

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

the observer:

    class Namespace_Modulename_Model_Observer {

    public function insertBlock($observer){

        $_block = $observer->getBlock();
        $_type = $_block->getType();
        if ($_type == 'catalog/product_price') {
            $_child = clone $_block;
            $_child->setType('some/block');
            $_block->setChild('kiddo', $_child);
            $_block->setTemplate('namespace/modulname/additional.phtml');
        }
    }
}

the current frontend output of said module

How would you advise to do that, preferably in canonical Magento API way ?

3
  • 1
    Any reason why you are doing this via an observer and not layout xml? Oct 24, 2014 at 11:54
  • Hi, it needs to be done via extension and to be theme independent ... Nov 10, 2014 at 15:44
  • You can add a layout file as a resource with an extensions config.xml. Put your layout xml under base/default which all themes fallback to and it will always be applied (and thus be theme independent). Nov 10, 2014 at 15:49

1 Answer 1

3

This is not the preferably way to extend the default magento layout. Its better to use the layout xml in your theme folder. This is not very efficient because this event is fired a lot of times on all the magento pages.

Its better to use something like this:

<catalog_product_view translate="label">
   <reference name="product.clone_prices (or some other block)">
      <block type="some/block" name="kiddo" template="namespace/modulname/additional.phtml">
   </reference>
</catalog_product_view>

Then add in your custom template the following line:

<?php echo $this->getChildHtml('kiddo'); ?>
1
  • Hi, thanks for the reply, but it needs to be done via extension and to be theme independent. Nov 10, 2014 at 15:43

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.