2

I'm getting all products with this code:

$collection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->addAttributeToFilter('size', array('in'=>$size))
->addAttributeToFilter('room', array('in'=>$room))
->addStoreFilter()
->setOrder('created_at', 'desc')
->setPage($pageNumber, $productPerPage)
->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
$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.

2

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

$productCollection = Mage::getResourceModel('catalog/product_collection')
                           ->addCategoryFilter($category);
  • 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
1

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')
            ->setStoreId(Mage::app()->getStore()->getId())
             ->addCategoryFilter($category);

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.