3

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.

6

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

| improve this answer | |
  • I have flat mode enabled and it is not working – Abhishek Dhanraj Shahdeo Aug 20 '16 at 5:03
  • Print Query Using ` echo $collection->getSelect() ` and check which query comes up – Murtuza Zabuawala Aug 20 '16 at 5:23
  • But, I'm getting only 6 entity_id, in the whole product collection of the category after I use something like: $products = Mage::getModel('catalog/category') ->load($categoryId) ->getProductCollection(); foreach($products->getData() as $product) { Mage::log($product['entity_id'], null, 'productId.log'); } – Abhishek Dhanraj Shahdeo Aug 20 '16 at 5:33
  • reindex And try again – Murtuza Zabuawala Aug 20 '16 at 5:38
  • Done that already. – Abhishek Dhanraj Shahdeo Aug 20 '16 at 5:39
2

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))
;
| improve this answer | |
2
$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.

| improve this answer | |
  • I have flat mode enabled and it is not working – Abhishek Dhanraj Shahdeo Aug 20 '16 at 5:03
  • yes.. this will work if EAV mode is enabled else you will receive 'Call to undefined method Mage_Catalog_Model_Resource_Product_Flat::getEntityTablePrefix()' exception – Nico Aug 30 '17 at 22:06

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.