1

How do I pull and display categories according to my custom field "Top_Menu"

$subCats = Mage::getModel('catalog/category')
->addFieldToFilter('Top_Menu', array('eq'=>'146'))
->load($category->getId())
->getChildren();

I keep getting this error

Invalid method Mage_Catalog_Model_Category::addFieldToFilter(Array
(
    [0] => Top_Menu
    [1] => Array
        (
            [eq] => 146
        )

)
)
1

First off you need the category collection.

$catCollection = Mage::getModel('catalog/category')
                         ->getCollection();

Once you have the collection you then need to apply your filters

$catCollection->addAttributeToFilter("Top_Menu" , array('eq' => '146'));

This will give you all categories that have the "Top_Menu" Attribute value of '146'. After you've applied all the filters you required, you can then either load your entire categories collection by calling $catCollection->load() or iterate through them using a foreach loop ( This will automatically call load()).

However if you want to print a multi level menu using all categories who's 'Top_Menu' value is '146' you need to use a bit of recursion. See example links

https://stackoverflow.com/questions/14660262/magento-programatically-build-category-tree enter link description here

If you just apply the 'Top_Menu' filters to the collection then it should work

| improve this answer | |
  • what if I only want a category of just one id? $catCollection->addAttributeToFilter("Id" , array('eq' => 'xxxxx')); – nikhil carvalho Nov 17 '14 at 1:01
  • If you just wanted one category with a particular id just use: Mage::getModel('catalog/category')->load($someID); – Danny Dev Nz Nov 17 '14 at 1:04
  • Can you tell me how to use it in addAttributeToFilter()? Also how do I go about printing it on the screen? – nikhil carvalho Nov 17 '14 at 1:05
  • $catCollection->addAttributeToFilter("entity_id" , array("eq" => $id)); – Danny Dev Nz Nov 17 '14 at 1:07
  • How would you like it printed ? – Danny Dev Nz Nov 17 '14 at 1:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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