每当我使用搜索框搜索产品时,左侧都会有按类别划分的过滤器。创建此过滤器的代码是这样的:
<dl id="narrow-by-list">
<?php $_filters = $this->getFilters() ?>
<?php foreach ($_filters as $_filter): ?>
<?php if($_filter->getItemsCount()): ?>
<?php if($_filter->getName() != "Category"){ ?>
<dt><?php echo $this->__($_filter->getName()) ?></dt>
<dd>
<?php echo $_filter->getHtml() ?>
</dd>
<?php } endif; ?>
<?php endforeach; ?>
</dl>
这只显示过滤器中的主要类别,我想显示其子类别。我尝试使用以下代码以编程方式设置另一个类别:
<?php
$layer = Mage::getSingleton('catalog/layer');
$_categ = Mage::getModel('catalog/category')->load(5);
$layer->setCurrentCategory($_categ);
?>
......但没有改变。有什么想法吗?
答案 0 :(得分:3)
当您尝试从模板中设置图层单例的其他类别时,由于已经应用了所有处理,因此为时已晚。
您可以将文件app/code/core/Mage/CatalogSearch/Model/Layer.php
复制到app/code/local/Mage/CatalogSearch/Model/
并添加基本Mage_Catalog_Model_Layer::getCurrentCategory()
方法的修改版本,如下所示:
public function getCurrentCategory()
{
$category = $this->getData('current_category');
if (is_null($category)) {
if ($category = Mage::registry('current_category')) {
$this->setData('current_category', $category);
}
else {
$category = Mage::getModel('catalog/category')->load(5); // Put here the ID of the category you want to use as basis for category filtering
$this->setData('current_category', $category);
}
}
return $category;
}