2012-09-06 36 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

奇怪!我不知道大膽寫作被認爲是大喊大叫。 –