在Magento中按多个类别过滤产品

时间:2014-01-31 12:09:18

标签: php magento

我在Magento中过滤类别时遇到了一些问题。我使用的过滤方法只显示一个类别,但我需要显示更多。这是我的代码:

$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);

代码仅显示id=32的产品。不显示id=5类别的产品。

感谢您的帮助。

我的Magento版本是1.6.2

1 个答案:

答案 0 :(得分:4)

试试这个

$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->joinField('category_id',
    'catalog/category_product',
    'category_id',
    'product_id=entity_id',
    null,
    'left'
);
$products->addAttributeToFilter('category_id', array('in' => array(32,5)));
foreach ($products as $product) {

}