How to change template of category page depending on category level in code?
سؤال
I had a project where there was no pattern and I applied all changes of categories layout in Admin area (Catalog->Manage Categories->Custom Design
tab). Now I need to have the following:
Parent category will have a grid of all subcategories with their thumbnails and titles.
Subcategories will look pretty much as default ones - Name, description and product list/grid
So I would like to do this and wonder what is the right way.
المحلول
In your catalog/category/view.phtml
you can check the category level using this:
$this->getCurrentCategory()->getLevel()
If you want to decide this in the backend you could make a whole seperate template that displays all the subcategories and add the following to your custom layout updates:
<reference name="category.products">
<action method="setTemplate"><template>catalog/category/list.phtml</template></action>
</reference>
To read out all the subcategories you can do this:
$children = explode(',', $this->getCurrentCategory()->getChildren());
$collection = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('name')
->addAttributeToFilter('entity_id', array(
'in' => $children
))->load();
foreach ($collection as $category) {
echo $category->getName() . '<br />';
}
نصائح أخرى
You should be able to put an if block at the top of the catagory view template.
Something like:
<?php if ($this->IsTopCategory()) : ?>
// Insert subcategory grid logic here
<?php endif; ?>