Magento列出了不属于某一类别的产品

时间:2013-11-01 18:49:18

标签: php magento

我需要列出一个类别中的所有产品。在Magento中,您不需要为产品指定类别,因此我想列出所有不属于某个类别的产品。

这是我尝试的但它给了我一个错误:

$productCollection = Mage::getModel('catalog/product')->getCollection();

foreach($productCollection as $_product){

    $productId = $_product->getId();
    $product = Mage::getModel('catalog/product')->load($productId); 

    $currentCatIds = $_product->getCategoryIds();
    $categoryCollection = Mage::getResourceModel('catalog/category_collection')
                 ->addAttributeToSelect('name')
                 ->addAttributeToSelect('url')
                 ->addAttributeToFilter('entity_id', $currentCatIds)
                 ->addIsActiveFilter();
    $i = 0;

    foreach($categoryCollection as $cat){
      $i++;
      //echo $cat->getName().' '.$cat->getUrl();
    }   

    if($i) echo $product->getName(); 
}   

1 个答案:

答案 0 :(得分:2)

 $collection = Mage::getModel('catalog/product')->getCollection()
                    ->addAttributeToFilter('category_ids','');

请参阅Magento get a list of products that are not related to any category