2

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

17

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>
  • Hei Adam, can you guess why above solution does not sort couple of products in search list result in my case (so strange), but the the rest of the search result is sorting fine? – Zinat Mar 14 '16 at 12:03
-1

/app/design/frontend/Vendor/theme/Magento_CatalogSearch/layout/catalogsearch_result_index.xml

    <referenceBlock name="search_result_list">
        <action method="setDefaultDirection"><param>asc</param></action>
    </referenceBlock>
  • Welcome to Magento SE and thanks for your contribution. Unfortunately the answer is not really helpful for this question. The question was specific to Magento 1, your code is Magento 2. Also it seems to solve a different problem. – Fabian Schmengler Jul 12 '17 at 20:31

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.