1

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.

3
  • 1
    I'm not sure this is a solution, but to avoid this issue I always do this. Create a new attribute (C), and have a cron that updates the value of attribute C with (A+arg1)*(B+arg2). This way I am able to take advantage of the default filtering system. The downside is that until you run the cron you might have false data in your filters. But I can live with that. If you don't like downside, you can add an observer that updates this attribute (C) when the product is saved or when an import is finished. – Marius Jun 18 '13 at 10:36
  • Instead of a cron job you could also use the eav backend_model function to update the value of attribute C before save based on the other attributes. This way the value for attribute C is always available at every update of the product. – Vladimir Kerkhoff Jun 18 '13 at 11:09
  • Thanks you very much for your answer. It just be noticed that the arg1 and arg2 are values provided by the user that want apply the filter. So, is it possible a batch calculation using a cron job? – Rocco Jun 19 '13 at 7:44
2

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.

1
  • Thanks you for your contribution. I tried to use addExpressionAttributeToSelect on the collection with other filters applied and them are lost. I’m not sure to have selected the right collection. The code is like this: $filteredCollection = Mage::getModel('catalog/product') ->getCollection() ->addExpressionAttributeToSelect('expr', …, …) ->addFieldToFilter('expr', array('lteq'=>$value)); Where expr is calculated as (A + arg1)*(B+arg2) and $value= arg3. I'm not sure to understand completely your other comments. Please, let me study them ... – Rocco Jun 19 '13 at 8:00

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.