1

I am trying to use a block from catalog/product_view in catalog/product_list but I can't get the block to render. Below is the code that I'm trying to call from template/catalog/product/list.phtml.

<?php echo $this->getChildHtml('additional') ?>

  • 1
    You have no kid called additional. Like you said yourself, they're different blocks, and while names like Bob and Alice are common, not every parent has a child with this names. Analogies aside, you need to define the additional block as child of the product list block in your template xml. This is explained in detail in [No Frills Magento Layout]( store.pulsestorm.net/products/no-frills-magento-layout) , which I cannot recommend often enough. – Melvyn Feb 3 '15 at 18:42
  • I added it to catalog.xml but I can't reference the product that is currently rendered. – Chris McKnight Feb 3 '15 at 19:20
  • It appears that using the Magento current product helper will work in my block. I'll look at implementing this later and let you know how it goes. – Chris McKnight Feb 13 '15 at 18:48
1

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.

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.