I am trying to sort the product listing by one attribute "best_seller". Attribute type is Yes/No , Now i need to sort product by the best_seller and need to show the best seller products set as Yes to appear first on the list. I already set this attribute to use for sorting from attribute settings.
I come to know that there is some issue with sorting when the attribute type is "Yes/No".
Can you anyone help me to fix this issue and or let me know if i am doing anything wrong?
Thanks
Best Answer
Short answer: The sorting doesn't work for Yes/No attributes.
Long Answer:
The problem:
When calling
addAttributeToSort
on a product collection, if the attribute has a source model then Magento callsaddValueSortToCollection
from that source model to sort the products.The
Yes/No
attributes use the source modelMage_Eav_Model_Entity_Attribute_Source_Boolean
that does not implement the methodaddValueSortToCollection
For this model the method just returns$this
without processing the collection.A possible solution:
You can rewrite the
Mage_Eav_Model_Entity_Attribute_Source_Boolean
and add youraddValueSortToCollection
method. It should basically be the same code as it is in the classMage_Catalog_Model_Product_Visibility
orMage_Catalog_Model_Product_Status
(not entirely sure about this) which is:Explanation provided here also for a similar issue