如果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-filters
to数组。$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 个文件。
- 配置文件
地点 :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
。
- Programmerrkt_Layerednav.xml
地点 :app/etc/modules/Programmerrkt_Layerednav.xml
<config>
<modules>
<Programmerrkt_Layerednav>
<active>true</active>
<codePool>local</codePool>
</Programmerrkt_Layerednav>
</modules>
</config>
这个文件激活我们的模块。
- 查看.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
谢谢