Collection visibility and status filters are deprecated, what should be used instead?

collections, deprecated, filter, magento, php

Solution

For Visibility there is (from Mage_Catalog_Model_Layer::prepareProductCollection()):

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

Which sets the CATALOG and BOTH filters to the collection.

For Status it appears a bit strange but still makes sense. In _initSelect in app/code/core/Mage/Catalog/Model/Resource/Product/Collection.php The following is done:

$this->getSelect()
            ->from(array(self::MAIN_TABLE_ALIAS => $this->getEntity()->getFlatTableName()), null)
            ->columns(array('status' => new Zend_Db_Expr(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)));

This code is executed when doing the

Mage::getResourceModel('catalog/product_collection')

So basically the status ENABLED is already checked when doing the

$category->getProductCollection()

Or similar product collection calls.

Problem

The `addVisibleFilterToCollection()` and `addSaleableFilterToCollection()` methods of `Mage_Catalog_Model_Product_Status` are annotated with @deprecated, but there is no instruction as to what approach to use instead. Code within Magento's core is still using those methods, ref `Mage_Catalog_Model_Layer::prepareProductCollection()`. What approach should be used decorating the collection with the correct visibility/salable filters?

Original source