magento - 分层导航过滤器中的子类别

标签 magento filter magento-1.7 categories layered-navigation

我有一个像这样简单的两级结构的类别:

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

目前要按子类别过滤 - 您必须先选择顶级类别。

如何在分层导航过滤器中显示所有顶级类别的子类别?

注意:子类别应受其他选定属性过滤器的影响。

最佳答案

在试验 Magento 文件时,我找到了问题的答案。

  1. 复制

    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;
    }
    

您可能有兴趣重写一些其他函数,例如 getResetValue 等。 我不得不重写模板以按主要类别对子类别进行分组。

结果(抱歉不能直接贴图):

之前: http://i.stack.imgur.com/skZpi.png

之后: http://i.stack.imgur.com/QxPhq.png

关于magento - 分层导航过滤器中的子类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23654430/

相关文章:

css - 在 magento 管理面板中添加新字体

php - Magento:添加自定义 EAV 属性,在创建实体时使用其默认值自动填充

mysql - Django 查询两次返回相同的对象

php - 如何在 Magento 1.7.0.2 中的product\view.phtml 中包含 jquery 选项卡

javascript - 使用带有字符串的另一个数组过滤带有对象的数组

java - Weka StringToWordVector 过滤器可以使用哪些类型的文本文件

php - Magento - 扩展开发,处理卸载

php - 如何使用 MySQL 查询更新产品重量和制造国家?

javascript - Magento jQuery 主题

magento - Varnish 缓存清空 Magento 中的购物车