开发者

Show only current category’s products in Product View

I’ve been struggling with this question for a long time and it seems very easy to solve, I just can’t do it by myself.

Take a look at this page: http://adegean.dominiotemporario.com/porcelanas-brancas/artigos-de-mesa/linha-americana/saladeira-pequena-americana.html

This product is associated with 2 different categories, and I would like to show only a list of products of this current category (in this case, ID 188), not from all the cats the product is listed in. It’s just something like filtering this list by “current_cat_Id” or something.

The current code is this:

    <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());

      ?开发者_JAVA百科>

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

Could someone please help me solving that?? Thank you in advance for your help!

Cheers, jw


Tks to http://www.magentocommerce.com/boards/viewthread/51638/ I finally came to an answer. The following code works great in he view.html page:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜