且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-08-26 15:35:52

您可以通过调用$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.