Question

I have flat mode enabled and I need to filter the product collection using category id, but I am not able to. Here below is my code...

$productCollection = Mage::getModel('catalog/product')
                ->getCollection()
                ->addAttributeToSelect('*')
                ->addFieldToFilter('type_id', array('eq' => 'simple'))
                ->addFieldToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED))
                ->addFieldToFilter('category_id', $brandValue['category'])
                ->addFieldToFilter('brands', $brandValue['brand']);

But, this is not filtering on the basis of the category.

Was it helpful?

Solution

I think Below code solve your concern

 $collection = Mage::getModel('catalog/category')
                    ->load($categoryId)
                    ->getProductCollection();

After that you can put your filters and all which attribute to load and all

OTHER TIPS

As an alternative you an get category's product collection as follows -

$category = new Mage_Catalog_Model_Category();
$category->load($brandValue['category']); // this is category id
$collection = $category->getProductCollection()
        ->addAttributeToSelect('*')
        ->addFieldToFilter('type_id', array('eq' => 'simple'))
        ->addFieldToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED))
;
$collection = Mage::getResourceModel('catalog/product_collection')
->joinField('category_id','catalog/category_product','category_id','product_id=entity_id',null,'left')
->addAttributeToFilter('category_id', array('in' => $catId))
->addAttributeToSelect('*')
->load();

In $catId pass the array of category ids which you want to filter.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top