Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 912
Jonathan Day Avatar asked Feb 04 '13 07:02

Jonathan Day


2 Answers

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.

like image 114
tomg Avatar answered Sep 29 '22 12:09

tomg


Did you tried common approach :

addAttributeToFilter('visibility',Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)

addAttributeToFilter('status',1)
like image 31
dagfr Avatar answered Sep 29 '22 13:09

dagfr