Exclude categories from category list of a product
-
13-12-2019 - |
Question
I added categories unordered list on product page with the following code
<?php
$currentCatIds = $_product->getCategoryIds();
$categoryCollection = Mage::getResourceModel('catalog/category_collection')
->addAttributeToSelect('name')
->addAttributeToSelect('url')
->addAttributeToFilter('entity_id', $currentCatIds)
->addIsActiveFilter();
foreach($categoryCollection as $cat){
echo '<li> <a href="'.$cat->getUrl().'" title="'.$cat->getName().' ">'.$cat->getName().'</a> </li>';
}
?>
I use some "service" categories and for this I set "not included in menu and in category list"
Is it possible to hide this "special categories" from the product detail list?
Solution
Add this to your filters:
->addAttributeToFilter('include_in_menu', array('eq'=>1))
OTHER TIPS
Try below code
<?php
$currentCatIds = $_product->getCategoryIds();
$categoryCollection = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('include_in_menu', array('eq'=>true))
->addIdFilter($currentCatIds)
->addIsActiveFilter();
?>
You will need to use ->addAttributeToFilter('include_in_menu', array('eq'=>true))
which is default attribute filter function in Magento.
For addIdFilter($currentCatIds)
, there is default function in category resource class.
Class: Mage_Catalog_Model_Resource_Category_Collection
Function: addIdFilter($categoryIds)
Param: array $categoryIds
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange