[英]I want to display categories and its products in list.phtml in Magento
I'm new in Magento and I need to display categories as heading and the list all products under this category. 我是Magento的新手,我需要将类别显示为标题,并列出此类别下的所有产品。 Somehow I got some relevant code while searching the web, but it is not working.
我在网上搜索时以某种方式获得了一些相关的代码,但是它不起作用。 Could display categories as heading but cannot display its corresponding products.
可以将类别显示为标题,但不能显示其相应的产品。
I will provide the code which i used to display the categories and products. 我将提供用于显示类别和产品的代码。
<?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 }
}
}
?>
What I am doing wrong? 我做错了什么? The code is written in
template/catalog/product/list.phtml
. 该代码写在
template/catalog/product/list.phtml
。
From my understanding ,i concluded with this solution : 根据我的理解,我得出以下解决方案:
<?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 -->
Here in the above code we have extracted all categories (its sub categories--->and its sub sub categories(just for reference)) 在上面的代码中,我们提取了所有类别(其子类别->及其子类别(仅供参考))
Every category is associated with <a>
tag. 每个类别都与
<a>
标记关联。
On clicking ,all the respective category's products would be displayed (in a list/grid pattern). 单击时,将显示所有相应类别的产品(以列表/网格模式)。
Common mistakes while managing categories. 管理类别时的常见错误。
For managing products check this link How to manage products 要管理产品,请检查此链接如何管理产品
Reasons for not displaying products. 不显示产品的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.