Many a time I have seen code where coders are doing all the product manipulation and work within PHP and not letting Magento do the work. Now, there is a caveat here that depending on the data you’re using, this could actually be quicker, but I think for the majority of cases, this is just lack of understanding.

So, I thought I’d knock up a quick post with some pointers on using the Magento Product Collection to simplify and speed up the data you’re getting.

The Product Collection class in Magento is a Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection. It should, in theory always return an array of product objects. The product objects it returns however are not the full product object you might be expecting, but it depends how you want to manipulate the data that comes back. Lets take a look at some of the functions we can use against it to get data.

Getting all products

So, how can we loop through this?

Filtering by status

This can be done in two ways:


You can actually use this as a basis for filtering any attribute. Simply replace “status” with the attribute you wish to filter by.

Filtering by Visibility

Filtering by Catgeory

Including / Excluding products

To do this, you can simply filter by ID. The false can be replaced with true, and means exclude / include.

Filter products from current website only

Filter products from current store only

Just get product IDs

Add category IDs to product object

Add the tiered pricing for a product to the collection

Image Credit: Victor Bezrukov