0

This is my category tree:

-Default Category
--Top Cat 1
---sub cat 1
----sub sub cat 1
----sub sub cat 2
----sub sub cat 3
---sub cat 2
---sub cat 3
--Top Cat 2
---sub cat 1
---sub cat 2
---sub cat 3
--Top Cat 3
---sub cat 1
---sub cat 2
---sub cat 3

The thing is I need to display different content and sidebar on the 3 Top Categories. How can I achieve this?

1

Each category has section called 'Custom Design'. From that section you can select a different theme for a category, add custom layout update statements, and select the layout of the page (1column, 3columns, ...). You can create a different theme containing only the files you need to change for the categories and use that one for your 3 top categories.

Also in the section 'Display settings' you can choose for 'Display mode' 'Static block only' if you don't want products listed (or 'Static blocks and products' if you want a static content and products listed) and create a static block with the content and design you want and use that for your categories. One block for each category.

  • Would that allow me to change the sidebar aswell? Is there anyway that I can do it automatically in case the end user wants to add a new top category? So that hedoesn't need to change template each time. – Elaine Marley Apr 3 '13 at 9:49
  • You can change the sidebar as well. Either from your new theme that you create only for the top categories, either through the xml layout update using '<refecence name="right (or left)">..</reference>'. There is no way on doing this automatically (without writing code). You ahve to do it when creating a category. – Marius Apr 3 '13 at 9:56
  • Yeah, I mean I code something so the user doesn't need to change a thing when he creates a new category. – Elaine Marley Apr 3 '13 at 10:06
  • You can simulate the values in the custom design and/or layout update fields through an observer on this event 'catalog_controller_category_init_after' your observer should get as a parameter the current category and you can manipulate its data for categories with level 2 (top categories). – Marius Apr 3 '13 at 10:10
4

Ideally you would set up an event observer and add a layout update handle to the view configuration analogous to the ID-specific handle which is added (link). Unfortunately, there are a number of handles processed after the earliest targeted events which would override any template settings in this handle. However, you can certainly use the dynamically-dispatched event controller_action_layout_render_before_catalog_category_view (link). This is a very late-in-the-rendering-game event, but its use is guarantees that your template change will be paramount. Because the category view action (indirectly) registers the viewed category, it's easy to perform the manipulation:

public function applyCategoryLevelTemplate()
{
    $category = Mage::registry('current_category');
    //ref https://github.com/benmarks/magento-mirror/blob/1.7.0.2/app/code/core/Mage/Catalog/controllers/CategoryController.php#L57

    $categoryViewBlock = Mage::app()->getLayout()->getBlock('category.products');
    //ref https://github.com/benmarks/magento-mirror/blob/1.7.0.2/app/design/frontend/base/default/layout/catalog.xml#L79
    //of course, pick the appropriate block(s) by name

    if ($categoryViewBlock && $category->getLevel() == 2) {
        $categoryViewBlock->setTemplate(/* e.g.custom_top_template.phtml */);
    }
}
1

The answer might be already in here: How to change template of category page depending on category level in code?

Using this:

$this->getCurrentCategory()->getLevel()

I can get the level of the category and I saw that all my top cats have level 2, now I can just loop through the content I need to display

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.