views:

104

answers:

2

Hi

I'm trying to find products that are in two categories. I've found an example to get products that are in category1 OR category2. http://www.alphadigital.cl/blog/lang/en-us/magento-filter-by-multiple-categories.html I need products that are in category1 AND category2.

The example in the blog is:

class ModuleName_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
  extends Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection{

  public function addCategoriesFilter($categories){

  $alias = 'cat_index';
  $categoryCondition = $this->getConnection()->quoteInto(
    $alias.'.product_id=e.entity_id AND '.$alias.'.store_id=? AND ',
    $this->getStoreId()
  );

  $categoryCondition.= $alias.'.category_id IN ('.$categories.')';

  $this->getSelect()->joinInner(
    array($alias => $this->getTable('catalog/category_product_index')),
    $categoryCondition,
    array('position'=>'position')
  );

  $this->_categoryIndexJoined = true;
  $this->_joinFields['position'] = array('table'=>$alias, 'field'=>'position' );

  return $this;

  }
}

When I'm using this filter alone it perform OR query on several categories. When I combine this filter with prepareProductCollection of Mage_Catalog_Model_Layer it somehow remove the filter effect.

How can I change the filter to AND and combine it with prepareProductCollection?

Thanks

Thanks

A: 

I think that it will be enough to call addCategoryFilter() twice on your product collection - once for each category. I have not tested it though, so might be wrong.

silvo
Unfortunately, The second call to addCategoryFilter() overwrites the first one.
pablo
A: 

I am also working on this to no avail, it was available in magento 1.3 using the attribute filter with finset on the category_ids column, however this was moved into the index table from the entity table and now no longer works.

There is one possible solution, but requries an override function which I found here

But this solution is far from ideal.

Hugh Wood
I think that your link use the old schema where all the categories were stored in a csv field like multi-select attributes are now. In django it's a line of code but in magento everything is hard.
pablo
The solution is in the comments I should of noted this before hand, it is difficult to get working and doesn't work straight away.
Hugh Wood