开发者

Magento products by categories

I try to get productsCollection by this code:

    $collection = Mage::getModel('catalog/category')->getCollection();
/* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */
$collection->addAttributeToSelect('url_key')
    ->addAttributeToSelect('name')
    ->addAttributeToSelect('is_anchor')
    ->addAttributeToFilter('is_active', 1)
    ->addIdFilter(array(4,5))//$_categories)
    ->setOrder('position', 'ASC')
    ->joinUrlRewrite()
    ->load();

$productCollection = Mage::getResourceModel('catalog/product_collection');
$layer             = Mage::getSingleton('catalog/layer');
$layer->prepareProductCollection($productCollection);
$productColl开发者_JS百科ection->addCountToCategories($collection);
foreach($productCollection as $product){
    print_r($product->getCategoryIds());
}

But line addIdFilter(array(4,5)) not work, and i see all product, even that no in some categorys.

What is wrong?


try passing a string with comma separated ids:

->addIdFilter("4,5")

If that does not work you can always try:

->addAttributeToFilter(’id’, array('in' => array(4,5))) 


This helped for me:

        $collection = $category->getCollection();
    /* @var $collection Mage_Catalog_Model_Resource_Category_Collection */
    $collection->addAttributeToSelect('url_key')
        ->addAttributeToSelect('name')
        ->addAttributeToSelect('all_children')
        ->addAttributeToSelect('is_anchor')
        ->addAttributeToFilter('is_active', 1)
        ->addIdFilter($category->getChildren())
        ->setOrder('position', Varien_Db_Select::SQL_ASC)
        ->joinUrlRewrite()
        ->load();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜