0

我的类别层次结构高达 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)。

4

1 回答 1

1

如果layered navigation只需要在 level-3 中显示,则需要在模板内设置渲染分层导航块的条件。

分层导航块由模板渲染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. 那是因为,magentoroot-category算作level-1

而已。那会成功的。享受编码。如果您有任何疑问,请告诉我。

编辑

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

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

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

显示分层导航内容。此方法在Mage_Catalog_Block_Layer_View类中定义,文件位于 location 中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-filter并且attributes-filter分别存储$filters。所以我们很幸运。我们需要做的是,只有当我们站在第 4 层时才包含attribute-filtersto数组。$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. 配置文件

地点 :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. 查看.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

谢谢

于 2014-06-17T02:31:40.020 回答