質問

I have a category with simple two-level structure like this:

Category #1
- Subcategory
- Subcategory
- ...
Category #2
- Subcategory
- Subcategory
- ...

Currently to filter by subcategories - you have to select top-level category first.

How to show subcategories of all top-level categories in layered navigation filter?

Note: Subcategories should by effected by other selected attribute filter.

役に立ちましたか?

解決

While experimenting with Magento files I've found the answer to my question.

  1. Copy

    app/code/core/Mage/Catalog/Model/Layer/Filter/Category.php
    to
    app/code/local/Mage/Catalog/Model/Layer/Filter/Category.php

  2. Open copied file. And replace _getItemsData with code below:

    /**
     * Get data array for building category filter items
     *
     * @return array
     */
    protected function _getItemsData()
    {
      $key = $this->getLayer()->getStateKey().'_SUBCATEGORIES';
      $data = $this->getLayer()->getAggregator()->getCacheData($key);
    
      if ($data === null) {
        // Get root category
        $root_category = Mage::getModel('catalog/category')->load(2);
    
        // Get main categories
        $data = array();
        $main_categories = $root_category->getChildrenCategories();
        foreach ($main_categories as $main_category) {
          if (!$main_category->getIsActive()) continue; // Ommit inactive
          // Get sub categories to list
          $sub_categories = $main_category->getChildrenCategories();
    
          // Add count to subcategories
          $this->getLayer()->getProductCollection()
            ->addCountToCategories($sub_categories);
    
          foreach ($sub_categories as $sub_category) {
            // Ommit inactive and zero product count sub categories
            if ($sub_category->getIsActive() || !$sub_category->getProductCount()) continue;
    
            // Output subcategories
            $data[] = array(
              'label' => Mage::helper('core')->htmlEscape($sub_category->getName()),
              'value' => $sub_category->getId(),
              'count' => $sub_category->getProductCount(),
              'parent' => $main_category->getName(), // Store parent name to group in template
            );
          }
        }
    
        $tags = $this->getLayer()->getStateTags();
        $this->getLayer()->getAggregator()->saveCacheData($data, $key, $tags);
      }
      return $data;
    }
    

You might be interested in rewriting some other functions such as getResetValue, etc. I had to rewrite template to group subcategories by main categories.

Result (sorry cant post images directly):

Before: https://i.stack.imgur.com/skZpi.png

After: https://i.stack.imgur.com/QxPhq.png

他のヒント

you can use the below code to show all subcategories of a current category in your sidebar or wherever

<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
    <ul>
        <?php foreach($_categories as $_category): ?>
            <li>
                <a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
                    <?php echo $_category->getName() ?>
                </a>
                <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
                <?php $_subcategories = $_category->getChildrenCategories() ?>
                <?php if (count($_subcategories) > 0): ?>
                    <ul>
                        <?php foreach($_subcategories as $_subcategory): ?>
                            <li>
                                <a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
                                    <?php echo $_subcategory->getName() ?>
                                </a>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                <?php endif; ?>
            </li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top