i created a module that adds a custom attribute to categories i want the defualt behavior when i call the category collection with Mage:getModel('catalog/category') to only return categories that have a specific value set for that custom attribute, how would I do this?

  • I do not have to worry about them not showing up in the backend – Yehuda Schwartz Jul 4 '16 at 15:20

When using:

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

You can call additional functions on that collection which will filter out the result:

$collection->addAttributeToFilter('your_custom_attribute_code', 'value');

Ofcourse you might also use the function chain like this:

Mage::getModel('catalog/category')->getCollection()->addAttributeToFilter('your_custom_attribute_code', 'value');

Now to change the default behavior of getCollection() you'd have to create a module that would have a model which rewrites the Mage_Catalog_Model_Category and Mage_Catalog_Model_Resource_Category, call a parent constructor in it and overwrite the getCollection() function to add the attribute filter by default. The easy and not the right way would be to simply copy the files from core to local namepool and apply the required changes in there.

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.