2010-09-30 294 views
2

我一直在努力解決這個問題很長一段時間,看起來很容易解決,我不能自己做。僅在產品視圖中顯示當前類別的產品

看看這個頁面:http://adegean.dominiotemporario.com/porcelanas-brancas/artigos-de-mesa/linha-americana/saladeira-pequena-americana.html

本品與2個不同的類別相關的,我想只顯示這個電流類別的產品列表(在這種情況下,ID 188),不來自產品列出的所有貓。它就像通過「current_cat_Id」或其他東西來過濾這個列表。

當前的代碼是這樣的:

<div class="box base-mini mini-related-items">  
    <?php 

    $test = Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); 
    echo 'Current Main Category of this product this list should show:'.$test; 

    ?> 


    <?php 

     if ($_product) { 
     // get collection of categories this product is associated with 
     $categories =$_product->getCategoryCollection() 

     //->setPage(1, 1) //selects only one category 
     ->addFieldToFilter('level','4')  //selects only 3rd level categories        
     //->addFieldToFilter('parent_id','188') //select only child categories of no 3       
     // ->setOrder("level") //combined by setPage, returns the lowest level category 
     ->load(); 

     // if the product is associated with any category 
     if ($categories->count()) 
     foreach ($categories as $_category) 
     { 

     $cur_category = Mage::getModel('catalog/category')->load($_category->getId()); 

     ?> 

     <div class="head"><h4>Todos os produtos da coleção <strong><?=$cur_category->getName()?> (Id: <?=$cur_category->getId()?>)</strong></h4></div> 
     <div class="content"> 
     <ol> 
     <? $products = Mage::getResourceModel('catalog/product_collection') 
     ->addCategoryFilter($_category) 
     ->addAttributeToSelect('small_image'); 

     foreach ($products as $productModel) 
     { 
       $_product = Mage::getModel('catalog/product')->load($productModel->getId()); 
       $width=50; $height=50; 
       $_imageUrl = $this->helper('catalog/image')->init($productModel, 'small_image')->resize($width, $height); 
     ?> 
<li<?php if($_product->isComposite() || !$_product->isSaleable()): ?> class="super-products"<?php endif; ?> class="product-box"> 
      <div class="product-images"> 

       <a href="<?php echo $_product->getProductUrl() ?>"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail')->resize(50) ?>" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" width="50" height="50" /></a> 
      </div> 
      <div class="product-details"> 
        <a href="<?php echo $_product->getProductUrl() ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a> 
       <!-- Price --> 
      <?php echo $this->getPriceHtml($_product, true) ?>   
      </div> 
     </li> 


     <? } 

     echo "</ol><div class=\"clear\"></div></div>"; 

     } 

     } 
     ?> 
      </div> 

可能有人請幫我解決了? 預先感謝您的幫助!

乾杯, JW

+0

那麼你現在有什麼問題?你能夠成功檢索當前類別嗎?你會得到什麼輸出? – 2010-09-30 19:24:54

回答

2

韓國社交協會於http://www.magentocommerce.com/boards/viewthread/51638/我終於來到了一個答案。以下代碼在view.html頁面中效果很好:

<div class="box base-mini mini-related-items"> 
<div class="head"><h4>Todos os produtos da coleção <strong><?php echo $this->getProduct()->getCategory()->getName() ?> </strong></h4></div> 
     <div class="content" style="float:left"> 
     <ol> 

<?php       
$cat_id = Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); // set current category id 
$category = Mage::getModel('catalog/category')->load($cat_id); 
$products = $category->getProductCollection()->addCategoryFilter($category)->addAttributeToSelect('*'); 
?> 
<?php foreach ($products as $_product): ?> 
<li<?php if($_product->isComposite() || !$_product->isSaleable()): ?> class="super-products"<?php endif; ?> class="product-box"> 
      <div class="product-images"> 

       <a href="<?php echo $_product->getProductUrl() ?>"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail')->resize(50) ?>" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" width="50" height="50" /></a> 
      </div> 
      <div class="product-details"> 
        <a href="<?php echo $_product->getProductUrl() ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a> 
       <!-- Price --> 
      <?php echo $this->getPriceHtml($_product, true) ?>   
      </div> 
     </li> 
<?php endforeach; ?> 
</ol></div><div style="clear:both"><br /></div> 
</div></div> 
相關問題