Custom Product Collection not Getting Filtered by

2019-05-26 20:26发布

问题:

I had Overridden the product List.php Class & here is the code

protected function _getProductCollection()
{   
  if (is_null($this->_productCollection)) {

    $result = array_unique($productIds);        

    $collection = Mage::getResourceModel('catalog/product_collection');
    $attributes = Mage::getSingleton('catalog/config')->getProductAttributes();
    $collection->addAttributeToSelect($attributes);
    $collection->addIdFilter($result);
    Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);

    $this->_productCollection = $collection;
    }

    return $this->_productCollection;
}

working fine, I also had added Layered Navigation as mentioned here and layered navigation appeared as expected.

The only problem is, when I click on any filter in layered navigation, navigation gets updated and filter also get added to url, but product list won't get filtered by the selected filter. Please guide me how can I apply the filters on product collection

回答1:

I could be wrong here, but your overridden _getProductCollection() method seems to be bypassing the layered navigation. I don’t know what your goal was that required you to do that, but the original version gets the product collection injected from the layered navigation model Mage_Catalog_Model_Layer:

protected function _getProductCollection()
{
    if (is_null($this->_productCollection)) {
        $layer = $this->getLayer();
        /* @var $layer Mage_Catalog_Model_Layer */
        if ($this->getShowRootCategory()) {
            $this->setCategoryId(Mage::app()->getStore()->getRootCategoryId());
        }

        // if this is a product view page
        ...

        $origCategory = null;
        if ($this->getCategoryId()) {
            $category = Mage::getModel('catalog/category')->load($this->getCategoryId());
            if ($category->getId()) {
                $origCategory = $layer->getCurrentCategory();
                $layer->setCurrentCategory($category);
            }
        }
        $this->_productCollection = $layer->getProductCollection();

        $this->prepareSortableFieldsByCategory($layer->getCurrentCategory());

        if ($origCategory) {
            $layer->setCurrentCategory($origCategory);
        }
    }
}

Perhaps you should revert to the original version of this method and see if layered navigation starts working, and if so then you know that you need to extend or incorporate this layer logic into your version.