如何在magento的模板页面(.phtml页面)中按类别id显示产品?
我是马根托的新手。我在allproduct.phtml文件中使用了下面的代码来获取所有类别id如何在magento的模板页面(.phtml页面)中按类别id显示产品?,magento,categories,product,Magento,Categories,Product,我是马根托的新手。我在allproduct.phtml文件中使用了下面的代码来获取所有类别id function get_categories(){ $category = Mage::getModel('catalog/category'); $tree = $category->getTreeModel(); $tree->load(); $ids = $tree->getCollection()->getAllIds(); $arr = array(); i
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;
}
现在我在一个数组中得到了下面想要的类别Id
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分隔的所有产品
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;
}
如何执行此操作?您可以通过调用
$category->getProductCollection()
来获取类别中的产品
样本:
$categories=Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('name')
foreach($categories作为$category){
$products=$category->getProductCollection()->addAttributeToSelect('name');
echo sprintf(“%d.%s”,$category->getId(),$category->getName());
foreach($products as$product){
echo sprintf(“%d.%s
,$product->getId(),$product->getName());
}
}
编辑:我故意把html标记弄错了,以防止它们被解析。您可以通过调用
$category->getProductCollection()
来获取类别中的产品
样本:
$categories=Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('name')
foreach($categories作为$category){
$products=$category->getProductCollection()->addAttributeToSelect('name');
echo sprintf(“%d.%s”,$category->getId(),$category->getName());
foreach($products as$product){
echo sprintf(“%d.%s
,$product->getId(),$product->getName());
}
}
编辑:我故意把html标记弄错了,以防止它们被解析。谢谢。我需要产品图片的网址和链接网址也。我怎么能得到那个?。请给我一些建议。我需要产品图片的网址和链接网址也。我怎么能得到那个?。请告诉我