Magento – How to add custom filter to the existing ones

filterlayered-navigation

I’m really new on Magento and I’ve to implement a custom filter on numeric value that involves more than one attribute. In other words, if a product have attributes A and B, I need to add filter on something like (A + arg1)*(B+arg2) < arg3, where arg1, arg2 and arg3 are input numeric values. I found the addExpressionAttributeToSelect to apply the filter expression to the product collection, but I’m not able to retrieve and take into account the already applied filters, e.g. the selected attributes in the Shop By section, returning the complete filtered product list as in normal layered navigation. What are the steps in the backend to implement the function?
I’m using Magento 1.7.0.2 CE.

Best Answer

I agree with the comment from Marius that precalculating C is probably a good idea.

Also, calling $collection->addAttributeToFilter() on a collection will add to existing filters, it won't replace them.

Nevertheless, if that isn't possible, you can inspect the collectios' select instance, to read already declared filters.

Please be aware that this would be a rather hackish approach, but since the collection interface doesn't give access to those protected properties, you have to either rewrite all product collection classes, or use this hack.

Check the three Mage_Catalog_Model_Resource_Layer_Filter_* classes applyFilterToCollection() methods on how the filters are applied to the collection's Zend_Db_Select instance.
use $collection->getSelect()->getPart($part) to access the relevant information.
Check the part constants in Zend_Db_Select for available keys.