Collection visibility and status filters are out of date, what should be used instead?

The addVisibleFilterToCollection() and addSaleableFilterToCollection() Mage_Catalog_Model_Product_Status annotated with @deprecated, but there is no instruction as to which approach to use instead. The code inside the Magento kernel still uses these methods, ref Mage_Catalog_Model_Layer::prepareProductCollection() .

What approach should be used to decorate a collection with the right visibility / sales filters?

+7
source share
3 answers

For Visibility, exists (from Mage_Catalog_Model_Layer :: prepareProductCollection ()):

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

Which installs the CATALONIA and BOTH filters in the collection.

For Status, this looks a little strange, but it still makes sense. In _init, select in app / code / core / Mage / Catalog / Model / Resource / Product / Collection.php The following is performed:

 $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 executed.

 Mage::getResourceModel('catalog/product_collection') 

So basically ENABLED status is already checked on execution

 $category->getProductCollection() 

Or similar product collection challenges.

+3
source

Have you tried the general approach:

 addAttributeToFilter('visibility',Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) addAttributeToFilter('status',1) 
+1
source

If you look at line 66

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

You will see that the legacy call is commented out and replaced with

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

This is how i use it my refactor

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

If you need more information about deprecated features, look here: http://freegento.com/doc/dc/d5b/_visibility_8php-source.html

0
source

All Articles