【问题标题】:magento attributes filter only in last (level 3) categorymagento 属性过滤器仅在最后一个(3 级)类别中
【发布时间】:2014-06-16 15:47:55
【问题描述】:

我的类别层次最高可达 3 级。例如,

Root Category 
Category 1
    Category 1.1
        Category 1.1.1
        Category 1.1.2
        Category 1.1.3
    Category 1.2
        Category 1.2.1
        Category 1.2.2
        Category 1.2.3
    Category 1.3
Category 2
.       .       .
.       .       .
.       .       .

问题 在分层导航中浏览类别时,产品的属性过滤器显示在所有层中,但我只想在最后一个类别(例如第三级类别)中进行属性过滤。如何做到这一点

注意:我已经搜索了很多,但在 magento meta 的一个问题上,一个答案是更改 css 以隐藏特定页面中的过滤器。但我想在更高的层次上做(没有css,如果可能的话,php hacks)。

【问题讨论】:

    标签: magento attributes e-commerce layered-navigation


    【解决方案1】:

    如果您只需要在第三层显示layered navigation,您需要在模板内设置一个渲染分层导航块的条件。

    分层导航块正在由模板app/design/frontend/<your_package>/<your_theme>/template/catalog/layer/view.phtml 呈现。你必须修改如下所示的内容

    <?php
    if(Mage::registry('current_category')->getData('level')== 4):
    ?>
          <?php 
             /**
               * Content in the template comes here :)
               *
               * Add content in this file, inside this condition
               */
          ?>
    <?php endif; ?>
    

    我们这里所做的是,我们使用Mage::registry('current_category')方法获取当前类别级别,并检查是否为level-4。如果是,则呈现内容。否则它不会。

    注意:您在上下文中描述为 3 级的内容实际上是 level-4。那是因为,magento 将root-category 视为level-1

    就是这样。那会成功的。享受编码。如果您有任何疑问,请告诉我。

    编辑

    因此您需要在所有类别级别中显示类别导航,但只需要在级别 4 中显示其他过滤器属性。如果是这样,让我们​​实现它。

    正如我已经说过的,app/design/frontend/&lt;your_package&gt;/&lt;your_theme&gt;/template/catalog/layer/view.phtml 是分层导航块的起点。在那个文件中你可以看到,它调用了下面的方法

     <?php $_filters = $this->getFilters() ?>
    

    显示分层导航内容。此方法在Mage_Catalog_Block_Layer_View 类中定义,文件位于app/code/core/Mage/Catalog/Block/Layer/View.php 位置。让我们分析一下这个方法。

    public function getFilters()
    {
        $filters = array();
        if ($categoryFilter = $this->_getCategoryFilter()) {
            $filters[] = $categoryFilter;
        }
    
        $filterableAttributes = $this->_getFilterableAttributes();
        foreach ($filterableAttributes as $attribute) {
            $filters[] = $this->getChild($attribute->getAttributeCode() . '_filter');
        }
    
    
        return $filters;
    }
    

    在这里你可以看到,当我们调用这个方法时,它返回一个数组,即$filters。您再次可以看到,category-filterattributes-filter 分别存储在 $filters 中。所以我们很幸运。我们需要做的是,仅当我们站在第 4 层时,才包含 attribute-filters$filters 数组。您可以通过更改此格式的代码轻松实现此目的。

     public function getFilters()
    {
        $filters = array();
        if ($categoryFilter = $this->_getCategoryFilter()) {
            $filters[] = $categoryFilter;
        }
    
        $current_layer = Mage::registry('current_category')->getData('level');
    
        if($current_layer == 4) {
            $filterableAttributes = $this->_getFilterableAttributes();
            foreach ($filterableAttributes as $attribute) {
                $filters[] = $this->getChild($attribute->getAttributeCode() . '_filter');
            }
        }
    
    
        return $filters;
    }
    

    就是这样。你完成了。但是更改核心文件并不是一个好习惯。所以为了保持核心文件不受影响,让我们创建一个小的module 来覆盖这个类。您的模块中只需要 3 个文件。

    1. config.xml

    位置:app/code/local/Programmerrkt/Layerednav/etc/config.xml

    <config>
        <modules>
            <Programmerrkt_Layerednav>
                <version>0.1.0</version>
            </Programmerrkt_Layerednav>
        </modules>
        <global>
            <blocks> 
                <!-- rewrites Mage_Catalog_Block_Layer_View -->
                <catalog>
                    <rewrite>
                        <layer_view>Programmerrkt_Layerednav_Block_Layer_View</layer_view>
                    </rewrite>
                </catalog>
            </blocks>
        </global>
    </config>
    

    config.xml 告诉 magento 我将覆盖核心块 Mage_Catalog_Block_Layer_View

    1. Programmerrkt_Layerednav.xml

    位置:app/etc/modules/Programmerrkt_Layerednav.xml

     <config>
        <modules>
            <Programmerrkt_Layerednav>
                <active>true</active>
                <codePool>local</codePool>
            </Programmerrkt_Layerednav>
        </modules>
    </config>
    

    这个文件激活我们的模块。

    1. View.php

    位置:app/code/local/Programmerrkt/Layerednav/Block/Layer/View.php

    <?php
    
    class Programmerrkt_Layerednav_Block_Layer_View extends Mage_Catalog_Block_Layer_View {
    
         /**
         * Get all layer filters
         *
         * This method Overwrites getFilter() method of class Mage_Catalog_Block_Layer_View
         *
         * Adds attribute filter only when category leve is 4
         *
         * @return array
         */
        public function getFilters()
        {
            $filters = array();
            if ($categoryFilter = $this->_getCategoryFilter()) {
                $filters[] = $categoryFilter;
            }
    
            $current_layer = Mage::registry('current_category')->getData('level');
    
            if($current_layer == 4) {
                $filterableAttributes = $this->_getFilterableAttributes();
                foreach ($filterableAttributes as $attribute) {
                    $filters[] = $this->getChild($attribute->getAttributeCode() . '_filter');
                }
            }
    
    
            return $filters;
        }
    }
    

    这将覆盖核心类。

    现在清除缓存并重新加载页面。检查它是否有效。请记住,您需要删除您对文件 view.phtml 所做的所有更改

    谢谢

    【讨论】:

    • 非常感谢。这样,整个左侧分层导航就会消失,包括属性过滤器和类别。但我想做的是,我只想在分层导航中显示类别..请帮助..
    • 我已经编辑了我的答案。请检查一下,让我知道它是否有效
    猜你喜欢
    • 2023-04-04
    • 1970-01-01
    • 2015-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-26
    • 2017-09-05
    相关资源
    最近更新 更多