Magento 1.9 – How to Change Sort Order for Search Results

magento-1.9searchsorting

i have made a new attribute called Best sellers (attribute code: search_order) so I can sort my products showing the best sellers at the top when someone searches for products. However when someone searches at the moment it is sorted by relevance and they have to select Best sellers. Is there a way i can change it so by default it is sorted by best sellers?

I tries to change the function setListOrder in result.php to

 public function setListOrders()
    {
        $category = Mage::getSingleton('catalog/layer')
            ->getCurrentCategory();
        /* @var $category Mage_Catalog_Model_Category */
        $availableOrders = $category->getAvailableSortByOptions();
        unset($availableOrders['position']);
        $availableOrders = array_merge(array(
            'search_order' => $this->__('Best sellers'),
            'relevance' => $this->__('Relevance')

        ), $availableOrders);

        $this->getListBlock()
            ->setAvailableOrders($availableOrders)
            ->setDefaultDirection('desc')
            ->setSortBy('search_order');

        return $this;
    }

but that just put Best sellers at the top of the sort by list but didnt actually sort the results

Thank you if you can help

Best Answer

I found the answer after some intense searching on Stack Overflow all i had to do was add these lines to my layout.xml

<catalogsearch_result_index>
    <reference name="search_result_list">
        <action method="setDefaultDirection"><param>asc</param></action>
        <action method="setSortBy"><param>attribute_you_want_to_use</param></action>
    </reference>
  </catalogsearch_result_index>
  <catalogsearch_advanced_result>
        <reference name="search_result_list">
            <action method="setDefaultDirection"><param>asc</param></action>
            <action method="setSortBy"><param>attribute_you_want_to_use</param></action>
        </reference>
    </catalogsearch_advanced_result>
Related Topic