Question

I have spent days looking for a simple tutorial about adding filters to a product collection. If anyone has a link to a good tutorial or explantion I would really appreciate it. I found a Magento Skin and the list.phtml file uses this code:

$products_collection = Mage::getModel( 'catalog/product' )
        ->getCollection()
        ->addAttributeToSelect( '*' )
        ->addCategoryFilter( $_cat )
        ->addOrder( 'position', 'ASC' );
    Mage::getSingleton( 'catalog/product_status' )->addSaleableFilterToCollection( $products_collection );
    Mage::getSingleton( 'catalog/product_visibility' )->addVisibleInCatalogFilterToCollection( $products_collection );
    Mage::getSingleton( 'cataloginventory/stock' )->addInStockFilterToCollection( $products_collection );

I have not seen filtering done with this method "addSaleableFilterToCollection". What other attributes can be used in that way? What are the possible reasons a developer would use that approach? The themes category list doesn't have reviews setup. I found the code that outputs the reviews but I think the reviews are missing from the product model. I tried

Mage::getSingleton( 'review/review' )->addReviewToCollection( $products_collection );

but that didn't do anything.

I'm more interested in learning how to fish instead of being handed a fish.

Was it helpful?

Solution

What other attributes can be used [to add custom filters]?

Literally any. It's up to the whim/needs of the developer or platform.

What are the possible reasons a developer would use that approach?

Primarily, DRY. It's likely that the filter conditions would need to be repeated in multiple contexts, and proper (sane) design tells us that the logic should be encapsulated in a method.

I tried Mage::getSingleton( 'review/review' )->addReviewToCollection( $products_collection ); but that didn't do anything.

While there is a lot of seemingly voodoo-y magic in Magento (usually stemming from the event architecture or Varien_Object::__call(), these collection filters have to be implemented somewhere. I recommend to search as below in app/code/ to find a given definition:

$ grep -srl 'n addSaleableFilterToCollection'

OTHER TIPS

For some general insight in the different options for collections in Magento please read this article

Some collections can be modified by using models to add filters, basically these models are just changing the query performed by Magento to retrieve the collection. You can use the following code to print to query of a certain collection to see whats happening

var_dump((string)$collection->getSelect());

The filters you've stated above pretty much cover all of these misc filters I think. Creating a review filter like your describing could be done by writing your own extension that has a method to join the review tables to the main collection query.

However, I'm not sure that would be a good course of action performance wise.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top