سؤال

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?

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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 */);
    }
}

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top