Jonathan Day
Jonathan Day

Reputation: 18692

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

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?

Upvotes: 7

Views: 1090

Answers (3)

TheRealJAG
TheRealJAG

Reputation: 2088

If you look at line 66

app/code/core/Mage/Catalog/Model/Product/Visibility.php

You will see the deprecated call is commented out and replaced with

$collection->setVisibility($this->getVisibleInCatalogIds());

Here is how I use it my refactor

$this->_itemCollection->setVisibility($this->getVisibleInCatalogIds());
// Deprecated: Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($this->_itemCollection);

If you want more information on deprecated functions, take a look here: http://freegento.com/doc/dc/d5b/_visibility_8php-source.html

Upvotes: 0

tomg
tomg

Reputation: 369

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.

Upvotes: 4

dagfr
dagfr

Reputation: 2384

Did you tried common approach :

addAttributeToFilter('visibility',Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)

addAttributeToFilter('status',1)

Upvotes: 1

Related Questions