I'm getting all products with this code:

$collection = Mage::getResourceModel('reports/product_collection')
->addAttributeToFilter('size', array('in'=>$size))
->addAttributeToFilter('room', array('in'=>$room))
->setOrder('created_at', 'desc')
->setPage($pageNumber, $productPerPage)
$collection->addAttributeToFilter('price', array('gt'=>$priceL));
$collection->addAttributeToFilter('price', array('lt'=>$priceG));

Then I added this code for filter by category:

->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id=entity_id', null, 'left')
->addAttributeToFilter('category_id', array('in' => $catID))

And now, this code work for filter by subcategory but doesn't work for root category. Why? And how do I solve this problem?

EDIT: I'm trying to filter by one category, multi category and root category.


You can try below code to get product collection for a particular category.

$productCollection = Mage::getResourceModel('catalog/product_collection')
  • Is it work for multi category or root category? – Ibrahim Mumcu Mar 4 '14 at 14:03
  • It will work for any category, just you need to provide category id in place of variable $category – Anshu Mishra Mar 4 '14 at 18:23
  • Maybe I'm doing something wrong, but this doesn't appear to work on 1.9 for root categories. – Yumecosmos Jul 31 '14 at 13:55
  • What exactly you need the output to be? – Anshu Mishra Aug 1 '14 at 4:44

You can use this one if you want the root category of current store.

$categoryId = Mage::app()->getStore()->getRootCategoryId();
$category = Mage::getModel('catalog/category')->load($categoryId);
$products = Mage::getResourceModel('catalog/product_collection')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.