【问题标题】:Magento - how to filter categories in phtmlMagento - 如何过滤phtml中的类别
【发布时间】:2014-09-07 21:15:13
【问题描述】:

我一直在制作魔法菜单。我得到了 phtml 中的集合,例如:

<?php $_helper = Mage::helper('catalog/category') ?> <?php $_categories = $_helper->getStoreCategories() ?> <?php $currentCategory = Mage::registry('current_category') ?>

现在我必须添加过滤器以显示特定类别。就像我有一个我想要显示的类别的 array(1,2,3,4)。那么如何将过滤器应用于此 Helper。

大家有什么建议请回答。

谢谢。

【问题讨论】:

    标签: magento collections categories php


    【解决方案1】:

    使用此代码

    <?php $catids[]=array(1,2,3,4);
    
     foreach($catids as $id):
    
                            $_category = Mage::getModel('catalog/category')->load($id);
    
                         if($_category->getIsActive()):
                                echo $_category->getName();
                         endif;
    endforeach;
    
    ?>
    

    如果有帮助,别忘了链接我的答案

    【讨论】:

      【解决方案2】:

      第一个答案是正确的,但效率不高,因为它消耗了不必要的数据库往返。 @Karan 的代码针对每个 id 向数据库发出查询。想象一下,如果要过滤的类别 id 的数量是 50 或以上。

      我的例子是这样的:

      <?php
      
      $catIds = array(1,2,3,4);
      
      $catCollection = Mage::getModel('catalog/category')->getCollection()->addAttributeToFilter('id', $catIds)->addAttributeToFilter('is_active',1);
      
      foreach($catCollection as $category){
        echo $category->getName()." ";
      }
      

      这会将数据库往返减少到只有一次。

      【讨论】:

      • 是的,你是对的。它减少了数据库查询之旅。谢谢:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-28
      • 2018-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-04
      相关资源
      最近更新 更多