Magento – Explanation on block needed

layoutmagento-1.8

I have recently started learning magento and would appreciate if anyone could explain what this does, especially the method addItemRender.

<adminhtml_sales_order_view>
 <block type="adminhtml/sales_order_view_items" name="order_items" template="sales/order/view/items.phtml">

                        <action method="addItemRender"><type>default</type><block>adminhtml/sales_order_view_items_renderer_default</block><template>sales/order/view/items/renderer/default.phtml</template></action>

                    </block>
</adminhtml_sales_order_view>

I do understand that it calls the method addItemRender with params $type, $block, $template on the block adminhtml/sales_order_view_items, but what I dont understand is what happens next and is it the same as below and calling getChildHtml() in the parent phtml ?

    <adminhtml_sales_order_view>
     <block type="adminhtml/sales_order_view_items" name="order_items" template="sales/order/view/items.phtml">

<block type="adminhtml/sales_order_view_items_renderer_default" name="order_items" template="sales/order/view/items/renderer/default.phtml"/>


                    </block>
</adminhtml_sales_order_view>

Any help would be really appreciated

Best Answer

Each order idem has a product associated to it, because that's what you buy...products.
And each product can have a different type: simple, configurable, grouped, bundle, downloadable, virtual.
Each product type may behave differently in certain conditions. For example the price should be shown differently depending on the product type.
The method addItemRenderer provides a block and a template for different product types.
To show a line in an order for a product the method Mage_Adminhtml_Block_Sales_Items_Abstract::getItemHtml is called.
This method checks the product type and looks in the associated renderers for a block and template to use.

public function getItemHtml(Varien_Object $item)
{
    if ($item->getOrderItem()) {
        $type = $item->getOrderItem()->getProductType();
    } else {
        $type = $item->getProductType();
    }

    return $this->getItemRenderer($type)
        ->setItem($item)
        ->setCanEditQty($this->canEditQty())
        ->toHtml();
}

This method calls getItemRenderer that looks if there is a block and a template associated to the product type. If there is it uses them. If not it uses the block and template marked as default.

The method is somehow similar to adding a child block to a parent block, but it's some kind of conditional child block. You have have multiple renderers associated to the parent block, but only one is used depending on what you are printing. And the renderer can be used multiple times. Each time a product of the same tipe is shown, the associated renderer is used.