Get All Sale Products From A Category in Magento

I recently was tasked with creating a landing page that took all the products that were on sale in a particular category. At first this seemed like a challenge because I was not sure how to get a collection of products from a particular category.

What I discovered that you can do is load a category model and then get a product collection and then continue to filter it down until you have the products that you are looking for. Here is the code that I used to achieve this:

$products = Mage::getModel('catalog/category')->load($category_id)
 ->getProductCollection()
 ->addAttributeToSelect('*')
 ->addAttributeToFilter('status', 1)
 ->addAttributeToFilter('visibility', 4)
 ->addAttributeToFilter('special_price', array('neq' => ""))
 ->setOrder('price', 'ASC')
 ;

What you will notice is that instead of loading a catalog/product, I loaded a catalog/category model. From there I got a product collection and then I put in some standard filters (only show products that have the proper visibility, are enabled, and whose special_price attribute does not equal blank. Finally I set the sort order to be the products price.

Overall this was very simple once I realized that I had to use the category model.

Leave a Reply

Your email address will not be published. Required fields are marked *