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

发布于 2024-09-25 10:51:27 字数 3409 浏览 2 评论 0原文

我已经在这个问题上苦苦挣扎了很长时间,看起来很容易解决,但我自己做不到。

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

有人可以帮我解决这个问题吗? 预先感谢您的帮助!

干杯, 杰威

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

      ?>

      <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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风苍溪 2024-10-02 10:51:27

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文