我是Magento的新手,我需要将类别显示为标题,并列出此类别下的所有产品。不知何故,我在搜索网页时得到了一些相关的代码,但它无法正常工作。可以将类别显示为标题但不能显示其相应的产品。
我将提供用于显示类别和产品的代码。
<?php
$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);
if ($cat->getIsActive()) {
$arr[$id] = $cat->getName();
$arr[$catId] = $cat->getId();
?>
<div class="right">
<div class="products">
<h2 class="head"><?php echo $arr[$id]; ?></h2>
<?php
$catagory_model = Mage::getModel('catalog/category')->load($id); //where $category_id is the id of the category
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addCategoryFilter($catagory_model); //category filter
$collection->addAttributeToFilter('status',1); //only enabled product
$collection->addAttributeToSelect(array('name','url','small_image')); //add product attribute to be fetched
//$collection->getSelect()->order('rand()'); //uncomment to get products in random order
$collection->addStoreFilter();
if(!empty($collection))
{
foreach ($collection as $_product):
?>
<div class="pro-list">
<h5><?php echo $_product->getname(); ?></h5>
<!-- other product details -->
</div>
<?php
endforeach;
}else
{
echo 'No products exists';
}
?>
</div>
</div>
<?php }
}
}
?>
我做错了什么?代码用template/catalog/product/list.phtml
编写。
答案 0 :(得分:0)
根据我的理解,我总结了这个解决方案:
<?php $helper = $this->helper('catalog/category') ?>
<?php $categories = $this->getStoreCategories() ?>
<div id="navigation_vert">
<?php if (count($categories) > 0): ?>
<ul>
<?php foreach($categories as $category): ?>
<li>
<a class="link" href="<?php echo $helper->getCategoryUrl($category) ?>">
<?php echo $this->escapeHtml($category->getName()) ?>
</a>
<?php $subcategories = $category->getChildren() ?>
<?php if (count($subcategories) > 0): ?>
<?php foreach($subcategories as $subcategory): ?>
<a href="<?php echo $helper->getCategoryUrl($subcategory) ?>">
<?php echo $this->escapeHtml(trim($subcategory->getName(), '- ')) ?>
</a>
<?php $subsubcategories = $subcategory->getChildren() ?>
<?php if (count($subsubcategories) > 0): ?>
<ul>
<?php foreach($subsubcategories as $subsubcategory): ?>
<li>
<a href="<?php echo $helper->getCategoryUrl($subsubcategory) ?>"><?php echo $this->escapeHtml(trim($subsubcategory->getName(), '- ')) ?></a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?><!-- subsubcategories if e -->
<?php endforeach; ?><!-- subcategories for e -->
<?php endif; ?><!-- subcategories if e -->
</li>
<?php endforeach; ?><!-- categories e -->
</ul>
<?php endif; ?><!-- count main categories if e -->
</div><!-- id navigation_vert e -->
在上面的代码中,我们提取了所有类别(其子类别---&gt;及其子子类别(仅供参考))
每个类别都与<a>
标记相关联。
点击后,将显示所有相应类别的产品(以列表/网格图案)。
常见错误while managing categories.
有关管理产品,请查看此链接How to manage products
不是 displaying products.