John Vincent
John Vincent

Reputation: 21

Magento: changing sort order in search, changes category page sort

I found several articles about how to change the sort order for search results in Magento, all roughly equal to this one: http://www.blog.magepsycho.com/how-to-change-default-sort-order-dir-in-magento-search-results/

Unfortunately this caused the sort order to change on category pages as well. Is there another way to do this? I want to leave category pages sorted by the default, Position -- but have search results sort by an attribute, part number. I'm hoping this is one of those "code will work better if you put it into this template" sort of things.

We are using Magento Enterprise ver. 1.9.0.0.

Upvotes: 2

Views: 6686

Answers (2)

Dont do that. Better on layout catalogsearch.xml

add

<reference name="search_result_list">
    <action method="setSortBy"><name>whateveryouwant</name></action>
</reference>

inside

<block type="catalogsearch/result" name="search.result" template="catalogsearch/result.phtml">

At the end after

<action method="setListOrders"/>
<action method="setListModes"/>
<action method="setListCollection"/> 

Upvotes: 1

PixieMedia
PixieMedia

Reputation: 1548

you can modify the catalogsearch results collection to order by whatever you want. By default search results are returned in order of relevance which is awarded by how close a match the search term is to the product search fields.

To modify the list order, copy this folder;

MAGE ROOT/app/code/core/Mage/CatalogSearch

To here;

MAGE ROOT/app/code/local/Mage/CatalogSearch

Now open this file;

MAGE ROOT/app/code/local/Mage/CatalogSearch/Block/Result.php

On around line 108, you will find this function;

 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(
        'relevance' => $this->__('Relevance')
    ), $availableOrders);

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

    return $this;
}

You can modify setSortBy(X) to what ever you want. By why wouldnt you want ot show the search results in order of relevance?

Upvotes: 1

Related Questions