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?


1 Answer 1


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 calls addValueSortToCollection from that source model to sort the products.
The Yes/No attributes use the source model Mage_Eav_Model_Entity_Attribute_Source_Boolean that does not implement the method addValueSortToCollection 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 your addValueSortToCollection method. It should basically be the same code as it is in the class Mage_Catalog_Model_Product_Visibility or Mage_Catalog_Model_Product_Status (not entirely sure about this) which is:

public function addValueSortToCollection($collection, $dir = 'asc')
    $attributeCode  = $this->getAttribute()->getAttributeCode();
    $attributeId    = $this->getAttribute()->getId();
    $attributeTable = $this->getAttribute()->getBackend()->getTable();

    if ($this->getAttribute()->isScopeGlobal()) {
        $tableName = $attributeCode . '_t';
                array($tableName => $attributeTable),
                    . " AND {$tableName}.attribute_id='{$attributeId}'"
                    . " AND {$tableName}.store_id='0'",
        $valueExpr = $tableName . '.value';
    else {
        $valueTable1 = $attributeCode . '_t1';
        $valueTable2 = $attributeCode . '_t2';
                array($valueTable1 => $attributeTable),
                    . " AND {$valueTable1}.attribute_id='{$attributeId}'"
                    . " AND {$valueTable1}.store_id='0'",
                array($valueTable2 => $attributeTable),
                    . " AND {$valueTable2}.attribute_id='{$attributeId}'"
                    . " AND {$valueTable2}.store_id='{$collection->getStoreId()}'",
            $valueExpr = $collection->getConnection()->getCheckSql(
                $valueTable2 . '.value_id > 0',
                $valueTable2 . '.value',
                $valueTable1 . '.value'

    $collection->getSelect()->order($valueExpr . ' ' . $dir);
    return $this;

Explanation provided here also for a similar issue

  • Facing Same problem, and implemented @Marius solution: rewrite Mage_Eav_Model_Entity_Attribute_Source_Boolean class but it shows me error Source model "eav/entity_attribute_source_boolean" not found for attribute "set_as_featured" with stack trace also. set_as_featured is in global scope with used in listing product and sorting Yes in backend options. please help me to solve this issue. Thanks. Commented Aug 11, 2014 at 5:28
  • Hi @Marius, I have implemented your code but when I am using $collection->addAttributeToSort('featured', 'desc'); then it is not returning correct result. For featured attribute I have set global scope, used in listing product yes and sorting No, because I don't want to add this option with sort-by options. Is there any other setting which are missing here? Commented Sep 12, 2016 at 11:59
  • I don't know if you are missing something. the code above worked for me.
    – Marius
    Commented Sep 12, 2016 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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