繁体   English   中英

过滤magento中的非活动类别

[英]Filtering non active categories in magento

因此,我使用此代码来显示网站上任何产品所包含的类别。问题是,我使用某些类别作为占位符来显示“精选产品”,我想尝试将其过滤掉。

这是PHP 5.6.30上的Magento 1.9.1.0

<ul class="listfix">
<?php $categories = $_product->getCategoryIds(); ?>
<?php foreach($categories as $k => $_category_id):
 ?>
<?php $_category= Mage::getModel('catalog/category')->load($_category_id)?>
     <?php if($_category->getId()):?> 
    <li><a href="<?php echo $_category->getUrl() ?>"><?php echo $_category-
>getName() ?></a>
    </li>
        <?php endif;?>
   <?php endforeach; ?>
</ul> 

我尝试添加

    $_category->addAttributeToFilter('is_active', 1)//get only active 
    categories

但是它引发了一个错误,我不是一个很棒的php专家,我只是找到了一段代码并尝试使其工作。 我从这里获得了原始零件

如下所示,我尝试添加以下代码,但仍然看到列出的类别无效...

<ul class="listfix">
<?php $activeCategories = 
Mage::getResourceModel('catalog/category_collection')
->addAttributeToFilter('is_active', 1)
->getColumnValues('entity_id');
?>  

<?php $activeCategories = $_product->getCategoryIds(); ?>
<?php foreach($activeCategories as $k => $_category_id):  ?>
<?php $_category= Mage::getModel('catalog/category')->load($_category_id)?>
    <?php if($_category->getId()):?> 
<li><a href="<?php echo $_category->getUrl() ?>"><?php echo $_category-
>getName() ?></a>
</li>
    <?php endif;?>

这将为您提供1.9中所有有效类别ID的列表:

$activeCategories = Mage::getResourceModel('catalog/category_collection')
->addAttributeToFilter('is_active', 1)
->getColumnValues('entity_id');

然后循环遍历该数组,并通过ID加载类别或其他内容:

foreach($activeCategories as $id) {
  $cat = Mage::getModel('catalog/category')->load($id);
}
<ul class="listmem">
<?php $activeCategories = 
Mage::getResourceModel('catalog/category_collection')
->addAttributeToFilter('is_active', 1)
->getColumnValues('entity_id');?>

<ul class="listfix">
<?php $activeCategories = $_product->getCategoryIds(); ?>
<?php foreach($activeCategories as $id) {
    $cat = Mage::getModel('catalog/category')->load($id);
} ?>

<?php if($cat->getId()):?> 
    <li><a href="<?php echo $cat->getUrl() ?>"><?php echo $cat->getName() ?>
</a>
    </li>
<?php endif;?>
</ul> 

我以为我可以弄清自己做错了什么,但是我仍然看到列表中未激活的类别。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM