如何在magento的模板页面(.phtml页面)中按类别ID显示产品?

时间:2012-05-29 14:41:27

标签: magento categories product

我是magento的新手。我在allproduct.phtml文件中使用了以下代码来获取所有类别ID。

function get_categories(){

$category = Mage::getModel('catalog/category'); 
$tree = $category->getTreeModel(); 
$tree->load();
$ids = $tree->getCollection()->getAllIds(); 
$arr = array();
if ($ids){ 
foreach ($ids as $id){ 
$cat = Mage::getModel('catalog/category'); 
$cat->load($id);
$arr[$id] = $cat->getName();
} 
}

return $arr;

}

现在我在一个数组中得到了如下所示的类别,

Array
(
    [Root Catalog] => 1
    [Default Category] => 2
    [Multivitamins] => 3
    [Vitamins and Minerals] => 4
    [Joints and Arthritis] => 5
    [EFA's] => 6
    [Diet and Digestion] => 7
    [Mood, Mind and Specialty] => 8
    [cardiostrong™] => 9
    [Teas and Juices] => 10
    [Additional] => 11
)

现在我需要显示由上述类别ID分隔的所有产品。

我该怎么做?。

1 个答案:

答案 0 :(得分:1)

您可以致电$category->getProductCollection()

获取某个类别的产品

样品:

$categories = Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('name');

foreach ($categories as $category) { $products = $category->getProductCollection()->addAttributeToSelect('name'); echo sprintf("< h1>%d. %s", $category->getId(), $category->getName()); foreach ($products as $product) { echo sprintf("%d. %s< br />", $product->getId(), $product->getName()); } }

编辑:我故意使html标签错误,以防止它们被解析。