cancel
Showing results for 
Search instead for 
Did you mean: 

Product collection by selecting multiple categories

Product collection by selecting multiple categories


To All developers, Scenario is that the website have over 100s of main and sub categories with similar products for example category Honda , breaks , brand. I need the customer to select the multiple categories before they can reach the product. And no I can not use attributes because the structure is too complicated. Please help as I have spent almost 2 weeks trying to figure this out. Thanks
1 REPLY 1

Re: Product collection by selecting multiple categories

The following code has done the trick for others but I'm not exactly sure how to implement this. I will really appreciate if anyone can  assist me with this. 

 

1. Call your collection and filter by the attributes you want to filter (all but the category one). For example:
[sourcecode language=”php”]
$collection= Mage::getResourceModel(‘catalog/product_collection’)
->addFieldToFilter(‘is_saleable’,1)
->addFieldToFilter(‘type_id’, array(‘eq’ => ‘configurable’));
[/sourcecode]

2. After doing this, you can apply your multiple category filter as follows:
[sourcecode language=”php”]$categories_to_filter = array(‘3’, ‘4’, ‘5’);
$ctf = array();

foreach ($categories_to_filter as $k => $cat) {
$ctf[][‘finset’] = $cat;
}

$collection->joinField(‘category_id’, ‘catalog/category_product’, ‘category_id’, ‘product_id = entity_id’, null, ‘left’)
->addAttributeToFilter(‘category_id’,array($ctf));

[/sourcecode]