The block alias name additional
stands for the block Mage_Catalog_Block_Product_View_Attributes
which is responsible for showing additional data related to a product in a product view page.
If you inspect further about the block, you can see that, this block is not rendered directly inside product view page. Instead it is added to a block group detailed_info
and then uses this block group to show the additional
block.
With that said, let us look what are the steps that you need to take care in order to add this block in a product list page.
1.Add the block in product list page via layout update
You can do this using local.xml
file. This is how the code looks like
File : app\design\frontend\<package>/<theme>/layout/local.xml
<layout>
<catalog_category_default>
<reference name="product_list">
<block type="catalog/product_view_attributes" name="product.attributes" as="additional" template="catalog/product/view/attributes.phtml">
<action method="addToParentGroup"><group>detailed_info</group></action>
</block>
</reference>
</catalog_category_default>
</layout>
This will add the product.attributes
block to the product_list
block, but which uses the feature of addToParentGroup
. Due to this product.attributes
block can be rendered in the same way it is rendering in a product view block.
2. Render the Child Groups of product_list
block
Since we added product.attributes
block as group child of product list, we need to show the block by looping through group childs. You can do this like this.
File : app\design\frontend\<package>/<theme>/template/catalog/product/list.phtml
....
<?php foreach ($_productCollection as $_product):
....
<?php foreach ($this->getChildGroup('detailed_info', 'getChildHtml') as $alias => $html):?>
<div class="box-collateral <?php echo "box-{$alias}"?>">
<?php if ($title = $this->getChildData($alias, 'title')):?>
<h2><?php echo $this->escapeHtml($title); ?></h2>
<?php endif;?>
<?php echo $html; ?>
</div>
<?php endforeach;?>
....
<?php endforeach; ?>
Here you can see that, we are added the group child rendering code inside the foreach
loop which is responsible for looping through each products in a category product list.
3.Register the product in each loop
Now in order to make the product.attributes
block work, we need to register and unregister for each product loop. Why would it need ? The answer resides in app\design\frontend\<package>/<theme>/template/catalog/product/view/attributes.phtml
. You can see below code there.
$_product = $this->getProduct();
This call is actaully carrying out by Mage_Catalog_Block_Product_View_Attributes::getProduct()
. The method look like this.
public function getProduct()
{
if (!$this->_product) {
$this->_product = Mage::registry('product');
}
return $this->_product;
}
As you can see, the method get the product from Mage::registry()
. So it is important that, we need to Mage::register
the product which is currently being in the product list loop.
So inside list.phtml
, under the foreach loop of product collection, before calling the group child blocks, you need to do this registration. It looks somewhat like this.
<?php foreach ($_productCollection as $_product):
...
<?php
Mage::unregister('product');
Mage::register('product', $_product);
<?php foreach; ?>
Hope that makes sense.