0

每当我使用搜索框搜索产品时,左侧都会按类别筛选。创建此过滤器的代码如下:

<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);
?>

...但没有任何改变。有什么想法吗?

4

1 回答 1

3

当您尝试在模板中的图层单例上设置其他类别时,为时已晚,因为所有处理都已应用。

您可以做的是将文件复制app/code/core/Mage/CatalogSearch/Model/Layer.phpapp/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;
}
于 2013-06-17T08:17:30.260 回答