Swip
Swip

Reputation: 173

Magento: Product collection filtered by two categories

I am writing a module that loads for me a list of featured products. All featured products are seated in their own category + hidden category "featured". The script returns me an error.

On category view (list.phtml) I call for gettopproducts.phtml (which works fine):

<?php $currentCategory = Mage::registry('current_category'); ?>
<?php $_products = $this->getTopProducts($currentCategory); ?>
<?php echo $this->__('Available products: ').$_products->count(); ?>

From gettopproducts.phtml I call a function getTopProducts() of Gettopproducts.php passing a current category. In Gettopproducts.php I have this:

public function getTopProducts($currentCategory)
{
    $_productCollection = Mage::getResourceModel('reports/product_collection')
    ->addAttributeToSelect('*')
    ->addCategoryFilter($currentCategory)
    ->addAttributeToFilter('category_ids',array('finset'=>'87'));
    $_productCollection->load();
    return $_productCollection;
}

This row:->addAttributeToFilter('category_ids',array('finset'=>'87')); should add a second category filter (ID of the "featured" category). But when I use this, I get an error. When I remove this row: ->addAttributeToFilter('category_ids',array('finset'=>'87')); it works perfectly.

I am using Magento 1.7.2

Upvotes: 0

Views: 6062

Answers (2)

ClaudioC
ClaudioC

Reputation: 1545

For magento 1.7 this is what works for me:

$collection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('id')        
    ->addAttributeToFilter('visibility', 4)
    ->addAttributeToFilter('home_slider', array('neq' => ''))
    ->addAttributeToFilter('home_slider_value', $slide_num)
    ->addStoreFilter();
    //here pass an array() of CATEGORY IDs
    $catids = array('id_1,id_2, etc..'); 

    $statements = array();
    foreach ($catids as $categoryId){
        if (is_numeric($categoryId)){
         $statements[] = "{{table}}.category_id = $categoryId";
        }
    }

    $collection->distinct(true)
    ->joinField('category_id','catalog/category_product', null,'product_id = entity_id', implode(" OR ",$statements),'inner');

Upvotes: 0

Swip
Swip

Reputation: 173

I found out the reason. For Magento 1.4+, category IDs are not a member of the reports/product_collection.

This is the way how to get it: replace ->addAttributeToFilter('category_ids',array('finset'=>'87')); with:

$_productCollection->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left');
$_productCollection->addAttributeToFilter('category_id', array('in' => array('finset' => '87'))); 

So the code looks like this:

$_productCollection = Mage::getResourceModel('reports/product_collection');
$_productCollection->addAttributeToSelect('*');
$_productCollection->addCategoryFilter($currentCategory);
$_productCollection->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left');
$_productCollection->addAttributeToFilter('category_id', array('in' => array('finset' => '87')));        
$_productCollection->load();
return $_productCollection;

Upvotes: 1

Related Questions