Magento how to filter product collection using 2 or more category filters
16401
single,single-post,postid-16401,single-format-standard,ajax_fade,page_not_loaded,,qode-theme-ver-5.9,wpb-js-composer js-comp-ver-4.3.4,vc_responsive

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

perfect-search-20

28 Jul Magento how to filter product collection using 2 or more category filters?

Magento how to filter product collection using 2 or more category filters

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.

 

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.

For Magento version 1.4 and higher

If joinField is not working then you can try using joinTable instead.

Hope it helps. Thanks.

Jenny Tran
hatran260612@gmail.com
No Comments

Post A Comment