0

I have some problem with filtering categories in Magento. The filter method which I used, display only one category, but I need to display few more. Here is my code:

$products = Mage::getModel('catalog/product')->getCollection();
$products->joinField(
    'qty',
    'cataloginventory/stock_item',
    'qty',
    'product_id=entity_id',
    '{{table}}.stock_id=1',
    'left'
);
$products->addAttributeToFilter('qty', array('neq' => 0));
$products->addAttributeToFilter('type_id','simple');
$products->addAttributeToFilter('status', 1);
$products->addAttributeToFilter('visibility', 4);
$products->addAttributeToSelect('sku');
$products->addAttributeToSelect('name');
$products->addAttributeToSelect('description');
$products->addAttributeToSelect('short_description');
$products->addAttributeToSelect('url');
$products->addAttributeToSelect('image');
$products->addAttributeToFilter('price', array('gt' => 250));
$products->addAttributeToSelect('special_price');
$products->addAttributeToSelect('manufacturer');

$products->addCategoryFilter(Mage::getModel('catalog/category')->load(array('32,5')),true);

The code display only products from id=32. The products from category id=5 are not displayed.

0

2 Answers 2

1
->joinField('category_id', 'catalog/category_product', 'category_id', 
    'product_id = entity_id', null, 'left')
->addAttributeToFilter('category_id', array('in' => array($categoryId1, $categoryId2)));
5
  • But I have one joinField. Can I add another code into this field?
    – Adrian
    Jan 31, 2014 at 12:56
  • Of course you can Jan 31, 2014 at 12:59
  • I have information: wrong name of attribute, and code looks like this $products->joinField( 'category_id', 'catalog/category_product', 'category_id', 'qty', 'cataloginventory/stock_item', 'qty', 'product_id=entity_id', '{{table}}.stock_id=1', null, 'left' );
    – Adrian
    Jan 31, 2014 at 13:07
  • I have answer: stackoverflow.com/questions/21479781/…
    – Adrian
    Jan 31, 2014 at 13:14
  • This gives error if one product attached to two ore more category. This answer also gives error in the situation stackoverflow.com/questions/21479781/…
    – mageUz
    Jan 31, 2014 at 15:26
0

I managed to resolve this (after much trial and error) with the following code:

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('status', 1);
$collection->addAttributeToSelect(array('name','sku','price','small_image'));

// Filter by multiple categories
$collection->joinField('category_id','catalog/category_product','category_id','product_id=entity_id',null,'left');
$data_cats = $this->getRequest()->getParam('categories');
// Or $data_cats = array(85,86,87,88);

      $filter_cats = array();
      foreach ($data_cats as $value_cats) {
         $filter_cats[] = array(
         'attribute' => 'category_id',
         'finset'    => $value_cats
      );
}

$collection->addAttributeToFilter($filter_cats);

Hope this helps someone ;)

Not the answer you're looking for? Browse other questions tagged or ask your own question.