Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

addCategoryFilter() - make it work with more than one
 
Netismine
Sr. Member
 
Avatar
Total Posts:  174
Joined:  2008-10-28
 

Currently, when assigning category filter to collection, you can only filter collection by one category.
It would be great if it was programmed to be able filter collection by many categories - example - return collection of products that appear in both Shirts and Summer categories.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Aftab Naveed
Jr. Member
 
Avatar
Total Posts:  29
Joined:  2009-05-12
Sydney Australia
 

facing the same issue, how can I solve this??

 
Magento Community Magento Community
Magento Community
Magento Community
 
Netismine
Sr. Member
 
Avatar
Total Posts:  174
Joined:  2008-10-28
 

try

->addCategoryFilter(array('in' => 'CAT1_ID,CAT2_ID'))
 
Magento Community Magento Community
Magento Community
Magento Community
 
mauricioprado00
Member
 
Avatar
Total Posts:  63
Joined:  2009-03-11
Argentina
 

hello, this is the solution that i used in my own module:

class Hyr_Hyr_Model_Mysql4_Product_Collection extends Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection{
    
private function makeCategoryInCondition($ids_categories){
        $categoryCollection 
Mage::getResourceModel('catalog/category_collection');
        
$conditions = array();
        
$conditions[] $categoryCollection->getConnection()->quoteInto('cat_index.category_id IN(' .  $ids_categories ')'"");
        
$joinCond implode(' AND '$conditions);
        
$categoryCollection
            
->getSelect()
                ->
join(
                        array(
'cat_index' => $categoryCollection->getTable('catalog/category_product_index')),
                        
$joinCond
                        
//,array('cat_index_position' => 'position')
                    
)
        ;
        
$categoryCollection
            
->getSelect()
            ->
reset(Zend_Db_Select::COLUMNS)
            ->
distinct()
            ->
columns('cat_index.product_id')
        ;
        return (string)
$categoryCollection
            
->getSelect();
    
}
    
public function addCategoriesFilterByIds($ids_categories){
        
if(!$ids_categories)
            return 
$this;
        if(
is_array($ids_categories)){
            $ids_categories 
implode(','$ids_categories);
        
}
        $in_condition 
$this->makeCategoryInCondition($ids_categories);

        
$this->getSelect()->where('e.entity_id IN ('.$in_condition.')');
        
        
Mage::dispatchEvent('catalog_product_collection_apply_limitations_after', array(
            
'collection'    => $this
        
));

        return 
$this;
    
}
}

the generatad query looks like this:

SELECT `e`.* FROM `hyr_catalog_product_entity` AS `e`SELECT `e`.* FROM `hyr_catalog_product_entity` AS `eWHERE (entity_id IN (SELECT DISTINCT `cat_index`.`product_idFROM `hyr_catalog_category_entity` AS `eINNER JOIN `hyr_catalog_category_product_index` AS `cat_indexON cat_index.category_id IN(11,12,13,14,19,20,21,22,23WHERE (e.entity_type_id '3')))
i thinks its good enought. I found another solutions in the forum, but it makes a left join of catalog_product_entity with catalog_category_product_index, wich i think its totally wrong because a product in more than one category retuns duplicated rows.

I made this to list all products in a category/subcategory view block, so i use this method like this inside the template of category view:

$productCollection Mage::getResourceModel('catalog/product_collection');
$productCollection->addCategoriesFilterByIds($this->getCurrentCategory()->getChildren());
$productCollection->load();
foreach(
$productCollection as $product){
    $_product 
Mage::getModel('catalog/product')->load($product->getEntityId());
    
var_dump($prod->getName());
}

I hope this helps someone else i spend my hole day figuring it out.

of course this is the config

<config>
...
   <global>
....
            <
catalog_resource_eav_mysql4>
                <
rewrite>
                    <
product_collection>Hyr_Hyr_Model_Mysql4_Product_Collection</product_collection>
                </
rewrite>
            </
catalog_resource_eav_mysql4>
....
   </global>
</
config>
 
Magento Community Magento Community
Magento Community
Magento Community
 
mauricioprado00
Member
 
Avatar
Total Posts:  63
Joined:  2009-03-11
Argentina
 

If someone find a bug/improvement i hope to let me know.

 
Magento Community Magento Community
Magento Community
Magento Community
 
mauricioprado00
Member
 
Avatar
Total Posts:  63
Joined:  2009-03-11
Argentina
 
Netismine - 10 October 2010 12:36 PM

try

->addCategoryFilter(array('in' => 'CAT1_ID,CAT2_ID'))

public function addCategoryFilter(Mage_Catalog_Model_Category $category)

dont make a post of something that you dont even check.

 
Magento Community Magento Community
Magento Community
Magento Community
 
flground
Jr. Member
 
Total Posts:  1
Joined:  2011-12-20
 

Hi, Can you please let me know which files that need to be edited?

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
elastopr
Jr. Member
 
Total Posts:  1
Joined:  2013-05-08
 

\app\code\core\Mage\CatalogSearch\Model\Advanced.php

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top