5

I am trying to get a list of products that have a sale price that are only in certain categories. Right now I am trying to use a product collection to get this data. I am not sure how I would go about restricting the collection for particular categories only. Here is what I have so far:

$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('status', 1)
    ->addAttributeToFilter('visibility', 4)
    ->addAttributeToFilter('special_price', array('neq' => ""))
    ->addAttributeToFilter('discontinued', array('neq' => 1))
    ->setPageSize(10)
    ->setOrder('price', 'ASC')
    ;

The discontinued attribute is a custom attribute that we use so that products don't display but also don't 404.

Is there a way to use the product model and restrict to certain categories?

Josh Pennington
  • 6,418
  • 13
  • 60
  • 93
  • As a note, `special_price` might not be available with your query. This question and answer expound a bit: http://stackoverflow.com/questions/12054165/ – Krista K Feb 25 '13 at 20:33

1 Answers1

5

Figured it out. You start with the category and get the product collection from the category and then refine it down from there. In code it looks like this:

$products = Mage::getModel('catalog/category')->load(410)
    ->getProductCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('status', 1)
    ->addAttributeToFilter('visibility', 4)
    ->addAttributeToFilter('special_price', array('neq' => ""))
    ->addAttributeToFilter('discontinued', array('neq' => 1))
    ->setOrder('price', 'ASC')
    ;
Josh Pennington
  • 6,418
  • 13
  • 60
  • 93