Home » Magento, MySQL

Magento: How to filter product collection using 2 or more category filters?

31 December 2009 3,364 views 7 Comments Popularity: 35% Share/Bookmark

Suppose, you have a product collection and you want to filter it by category. Suppose, you want to filter it by more than one category. You can use addCategoryFilter if you have only one category. But, what if you want to filter by more than one category?

Category ids are stored for product in a comma separated way. So, to filter product collection by more than one category, you have to use:
addAttributeToFilter(‘category_ids’,array(‘finset’=>$categoryIds));

$categoryIds can be a single category id or comma separated category ids.

The following code filters product collection by two category ids (36 and 37).

 $_productCollection = Mage::getResourceModel('reports/product_collection')
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('category_ids',array('finset'=>'36,37'));

finset does so… when we use finset, the mysql function find_in_set is used in the sql query by Magento.

mysql function find_in_set:

FIND_IN_SET() looks for the first occurrence of a string within another string containing comma-separated values.

SELECT FIND_IN_SET(‘b’,'a,b,c,d’); // result = 2

Cheers!

From Mukesh Chapagain's Blog | Post Magento: How to filter product collection using 2 or more category filters?

Related posts:

  1. Magento: Get Product Collection by Type
  2. Magento: Get category name and url from product
  3. Magento: Get sub categories and product count
  4. Magento: Very Useful Collection Functions
  5. Magento 1.4: No products displayed in category listing

7 Comments »

  • Mosses said:

    Thanks. Will the same method work for filtering based on certain attribute. I have created a multiple select attribute with pre assigned values and been trying to list the all products having any value option of said attribute. Having issues filtering and listing for that matter. Here’s my code.

    $model = Mage::getModel(‘catalog/product’);
    $collection = $model->getCollection();
    $collection->addAttributeToSelect(‘my_attribute’);
    $collection->addAttributeToFilter(‘my_attribute’,'finset’=>(‘value_name’));
    $collection->load();

    Thanks,
    Mosses

  • admin said:

    yes, you should be able to do so.

  • Rik said:

    It looks like this doesn’t work in 1.4.*

    Seems the ‘category_ids’ field has been removed from the ‘catalog_product_entity’ table.

    Trying to figure out a new workaround – anyone have any ideas??

  • DCA said:

    Now the category ids are in catalog_category_product_index

    For example, if you want return all product categories your query should like this:
    SELECT * FROM catalog_category_product_index WHERE product_id = 123

  • Shahid said:

    As the filtering way has been changed in magento 1.4. So, can someone please give an example that how we can filter products by multiple categories in magento 1.4?

  • Ben Incani said:

    for version 1.4.x

    create a local copy and edit Collection.php
    e.g.
    code\local\Mage\Catalog\Model\Resource\Eav\Mysql4\Product

    create a new function using the guts of _applyProductLimitations()
    e.g.
    public function addCategoriesFilter(array $categories) {

    $conditions[] = $this->getConnection()->quoteInto(‘cat_index.category_id IN(‘ . implode(“,”, $categories) . ‘)’, “”);

    }

    then make your call
    e.g.
    $this->_productCollection = Mage::getModel(‘catalog/resource_eav_mysql4_product_collection’)
    ->setStoreId($storeId)
    ->addCategoriesFilter(array(9,11,44));

  • anshuman said:

    use addCategoryFilter instead

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.