我一直在maga菜单上工作。我在phtml中得到了如下集合:
<?php $_helper = Mage::helper('catalog/category') ?> <?php $_categories = $_helper->getStoreCategories() ?> <?php $currentCategory = Mage::registry('current_category') ?>
现在,我必须添加过滤器以显示特定类别。就像我有一个要显示的类别数组(1,2,3,4)。因此,如何将过滤器应用于此帮助器。
任何人有任何建议请回答。
谢谢。
第一个答案是正确的,但是效率不高,因为它消耗了不必要的数据库往返行程。@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()." ";
}
这样会将数据库往返次数减少到仅一次。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句