在产品视图中仅显示当前类别的产品

时间:2010-09-30 18:22:46

标签: magento

我长期以来一直在努力解决这个问题,似乎很容易解决,我自己也做不到。

请看这个页面: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

1 个答案:

答案 0 :(得分: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>