In one of our projects, the requirement was to have a different layout for some category pages. So, we decided to use a different template file for these categories. We added the following code in the “Custom Layout Update” field for these categories in admin.
<reference name="category.products"> <action method="setTemplate"> <template>catalog/category/custom-category.phtml</template> </action> </reference>
These category pages need to show the products in a different layout.
We can get the category object in the “custom-category.phtml” file as shown below and access the category data.
$_category = $this->getCurrentCategory();
Now the next step was to get the category products collection.
The layout handle “catalog_category_default” has the following code
<catalog_category_default translate="label"> . . . <reference name="content"> <block type="catalog/category_view" name="category.products" template="catalog/category/view.phtml"> <block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml"> . . . </block> </block> </reference> </catalog_category_default>
So the need was to get child block “product_list” in the “custom-category.phtml” file
Here is the code that I used get the child block and load the product collection
<?php $productBlock = $this->getChild('product_list'); $_productCollection = $productBlock->getLoadedProductCollection(); ?>
Summary
- You can use the “getChild()” method defined in {{MAGENTO_ROOT}}app/code/Mage/Core/Block/Abstract.php to retrieve child block by name.
- If you pass the block name like “$this->getChild(‘product_list’)” it returns that block object.
- If you do not pass anything to the “getChild()” method it returns all the child blocks.
- $this->getChildHtml() returns the html content of all child blocks
Leave a Comment