如何在magento的模板页面(.phtml页面)中按类别ID显示产品? [英] How do display products by category id in template page(.phtml page) in magento?

查看:74
本文介绍了如何在magento的模板页面(.phtml页面)中按类别ID显示产品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i am new to magento. i used below code in allproduct.phtml file for get all category id's.

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;

}

现在我在一个数组中得到了想要的类别

now i got category Id'd like below in one array,

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分隔的所有产品.

Now i need to display all the products seperated by the above category id's.

我该怎么做?.

推荐答案

您可以通过调用$category->getProductCollection()来获得类别中的产品.

You can obtain the products in a category by calling $category->getProductCollection().

示例:

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

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

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标记弄错了,以防止它们被解析.

edit: I made the html tags wrong on purpose, to prevent them from being parsed.

这篇关于如何在magento的模板页面(.phtml页面)中按类别ID显示产品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆