2
<?php
$_category = $this->getCurrentCategory(); $_categoryCollection = Mage::getResourceModel('catalog/category_collection')
                 ->addCategoryFilter($_category)
                 ->addAttributeToSelect('*')
                 ->addAttributeToSelect('is_brand','yes')
                 ->addAttributeToFilter('level','5')
                 ->addIsActiveFilter();
foreach($_categoryCollection as $cat){?>
<ui>
<?php //display current_Category image
     if($cat->getImageUrl()): ?>
      <img src="<?php echo $cat->getImageUrl(); ?>" />
<?php endif; ?>
     <a href="<?php echo $cat->getUrl(); ?>">
<?php //display current_Category image
     echo $cat->getName(); ?>
     </a>
    </ul>
<?php   
        }
        ?>

I want to display categories which have the value "yes" for the category attribute "is_brand". and i want it to be fetched from the current category. I could not access getCurrentCategory() inside catalog/category_collection..

i need only to fetch the category collection of level 5 only in the current category. THE ABOVE CODE FETCHES COLLECTIONS WITH LEVEL 5 FROM ALL CATEGORIES.

2
  • Please explain your question! Commented Feb 10, 2015 at 10:34
  • Question Elaborated @ Rahul Kumar Das Commented Feb 10, 2015 at 10:41

2 Answers 2

1

Give this a try:

$_category = $this->getCurrentCategory(); 
$path = $_category->getPath();
$_categoryCollection = Mage::getResourceModel('catalog/category_collection')
             ->addAttributeToFilter('path', array('like' => $path.'/%'))
             ->addAttributeToSelect('*')
             ->addAttributeToFilter('is_brand','1')
             ->addAttributeToFilter('level','5')
             ->addIsActiveFilter();
3
  • Fantabulous Marius... Perfect mage hack.. Commented Feb 10, 2015 at 12:42
  • 1
    It's not a hack...is the way to do it. :)
    – Marius
    Commented Feb 10, 2015 at 12:45
  • :).........................
    – Amit Bera
    Commented Feb 10, 2015 at 12:50
1

Please remove ->addCategoryFilter($_category) which not exclude current category from Category collection

This addCategoryFilter($_category) only filter the product collection filter by category.

Use addAttributeToFilter() to filter the category by path field.Level 5 category must included current category path According to magento

 $_categoryCollection = Mage::getResourceModel('catalog/category_collection')
                     ->addAttributeToSelect('*')
                     ->addAttributeToSelect('is_brand','yes')
                     ->addAttributeToFilter('level','5')
                     ->addIsActiveFilter()
 ->addAttributeToFilter('path', array('like' => $_category->getPath().'/%'));

You can use addIdFilter(categoryIds) if you want specific ids categories

7
  • I do not want to manually add the entity ID Amit..Can you elaborate your answer please? Commented Feb 10, 2015 at 10:40
  • first clear , are you want the category collection of level 5 with exclude current category
    – Amit Bera
    Commented Feb 10, 2015 at 10:43
  • i need only to fetch the category collection of level 5 only in the current category. Commented Feb 10, 2015 at 11:49
  • goto it... your question .. shorly give you solution
    – Amit Bera
    Commented Feb 10, 2015 at 11:54
  • Can you send some predictions?? Commented Feb 10, 2015 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.