Magento 1.7 Filter products by multiple categories

后端 未结 2 670
甜味超标
甜味超标 2021-01-16 08:09

I am looking for a way to filter the products being returned on a category page by the current category AND an optional sub-category. Every solution I have seen so far has b

2条回答
  •  不思量自难忘°
    2021-01-16 08:54

    look here: http://vibrantdrive.com/how-to-filter-magento-products-using-2-or-more-category-filters/

    To get products in Category 4 AND category 5

    $_productCollection = Mage::getModel('catalog/product')
     ->getCollection()
     ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
     ->addAttributeToSelect('*')
     ->addAttributeToFilter('category_id', array(
         array('finset' => '4'),
         array('finset' => '5'))
     )
     ->addAttributeToSort('created_at', 'desc');
    

    To get product in Category 4 OR category 5

    $_productCollection = Mage::getModel('catalog/product')
     ->getCollection()
     ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
     ->addAttributeToSelect('*')
     ->addAttributeToFilter('category_id', array(
         array('finset' => array('4', '5')),
     )
     ->addAttributeToSort('created_at', 'desc');
    

提交回复
热议问题