Paulo
Paulo

Reputation: 115

How to remove configurable products from category list ONLY

I need to show only simple products in category page, but I cant set "not visible" to the configurable because I need the an active product page from the configurables.

I found this code to remove the configurables from the listing:

  $_productCollection=$this->getLoadedProductCollection();
  $_productCollection = clone $this->getLoadedProductCollection();
  $_productCollection->clear()
                     ->addAttributeToFilter('type_id', 'simple')
                     ->load();

  $_helper = $this->helper('catalog/output');

It works, but, In the layered navigation the configurable product are still counting. Its like "Color: Red (2)" but I just have 1 red (simple). How can I remove completly the configurable products?

Upvotes: 1

Views: 1354

Answers (1)

Oleg Ishenko
Oleg Ishenko

Reputation: 2233

The layered navigation uses a collection object which is loaded separately.

One possible way to ensure correct counts next to the navigation filters is to override model Mage_Catalog_Model_Layer and add your filter to its function Mage_Catalog_Model_Layer::prepareProductCollection

public function prepareProductCollection($collection)
    {
        $collection
            ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
            ->addMinimalPrice()
            ->addFinalPrice()
            ->addTaxPercents()
            ->addUrlRewrite($this->getCurrentCategory()->getId())
            ->addAttributeToFilter('type_id', 'simple');

        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);

        return $this;
    }

To achieve this create a module in your local code pool. In the config.xml file add the following node into the global node

<models>
        <catalog>
            <rewrite>
                <layer>YourPackage_YourModule_Model_Rewrite_Layer</layer>
            </rewrite>
        </catalog>
</models>

In your module add a directory 'Rewrite' under folder 'Model' and create a file Layer.php in it. In the created file Model/Rewrite/Layer.php add a class with the following definition:

class YourPackage_YourModule_Model_Rewrite_Layer extends Mage_Catalog_Model_Layer {
}

Add the function above into this class, clear cache.

Upvotes: 2

Related Questions