我是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分隔的所有产品。
我该怎么做?。
答案 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标签错误,以防止它们被解析。