Question

Whenever I search a product using the search box, on the left side there is a filter by category. The code that creates this filter is this one:

<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>

This shows only the main category in the filter, and I'd like to show its subcategories. I tried to set another category programmatically with this code:

<?php
     $layer = Mage::getSingleton('catalog/layer');
     $_categ = Mage::getModel('catalog/category')->load(5);
     $layer->setCurrentCategory($_categ);
?>

... but nothing changed. Any thoughts?

Was it helpful?

Solution

When you try to set the other category on the layer singleton from a template, it's too late as all the treatments were already applied.

What you can do is to copy the file app/code/core/Mage/CatalogSearch/Model/Layer.php into app/code/local/Mage/CatalogSearch/Model/ and to add a modified version of the base Mage_Catalog_Model_Layer::getCurrentCategory() method, looking like this :

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top