2012-09-06 41 views
2

显示在list.phtml类及其产品我米在Magento新的,我需要这个类别下的标题和列表中显示类的所有产品。不知何故,我搜索了一些相关的代码,但它不起作用。可以将类别显示为标题,但无法显示其相应的产品。我将提供我用来显示类别和产品的代码。我想在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 } 
     } 
     } 
     ?> 

我做错了什么? 代码编写模板/目录/产品/ list.phtml 有人可以请帮助,并解释如何做到这一点。

+0

能否请您解释一下你的意思是当你说“显示类别,标题和目录此类别下的所有产品” ?您是否需要在单个页面上显示所有类别与他们的产品? – 1000Nettles

+0

默认情况下,Magento将显示类别作为标题以及该类别中列出的所有产品。你不需要为此编写任何代码。如果您使用目录>类别,则可以管理该类别的设置(查看该类别是否处于活动状态,查看该类别中列出的产品等等)。 –

回答

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 --> 


在这里,我们已经提取的所有类别上面的代码(它的子类 - >和它的子子类(只为参考))
每个类别都与<a>标签相关联。

上点击,将显示所有相关类别的产品(在列表/网格图案)。

常见的错误while managing categories.

为了管理产品检查此链接How to manage products

理由不 displaying products.

+0

请不要大喊(又名:大胆的一切)在我们 – kleopatra

+0

奇怪!我不知道大胆写作被认为是大喊大叫。 –