I want to sort my catalog search result by multiple attributes.

For default sorting by price I have added below code in xml file :

    <reference name="search_result_list">
        <action method="setDefaultDirection"><param>asc</param></action>
        <action method="setSortBy"><param>price</param></action>

This is working correctly.

I have a custom attribute says "Is Special" (Yes/No) for product and want to show all special products on top in catalog listing with price asc order and then other product with price asc order.

For this I have tried below code in Custom_Module_Model_CatalogSearch_Layer

$collection->addAttributeToSort('special', 'DESC');
$collection->addAttributeToSort('price', 'ASC');

Sorting by only special attribute $collection->addAttributeToSort('special', 'DESC') working but not working in combination as I want.

How to do that?

EDIT : result of $collection->getSelect()->__toString()

SELECT `e`.*, `search_result`.`relevance`, `price_index`.`price`, `price_index`.`tax_class_id`, `price_index`.`final_price`, IF(price_index.tier_price IS NOT NULL, LEAST(price_index.min_price, price_index.tier_price), price_index.min_price) AS `minimal_price`, `price_index`.`min_price`, `price_index`.`max_price`, `price_index`.`tier_price`, `cat_index`.`position` AS `cat_index_position` FROM `catalog_product_entity` AS `e`
 INNER JOIN `catalogsearch_result` AS `search_result` ON search_result.product_id=e.entity_id AND search_result.query_id='55'
 INNER JOIN `catalog_product_index_price` AS `price_index` ON price_index.entity_id = e.entity_id AND price_index.website_id = '1' AND price_index.customer_group_id = 0
 INNER JOIN `catalog_category_product_index` AS `cat_index` ON cat_index.product_id=e.entity_id AND cat_index.store_id='1' AND cat_index.visibility IN(3, 4) AND cat_index.category_id = '2'
 LEFT JOIN `catalog_product_entity_int` AS `special_t` ON e.entity_id=special_t.entity_id AND special_t.attribute_id='132' AND special_t.store_id='0' ORDER BY `special_t`.`value` DESC, `price_index`.`min_price` ASC
  • Possible duplicate of Adding more options to 'sort by ' in product listings Sep 12 '16 at 11:16
  • Hello @fschmengler, I don't want to add this option in drop down box instead of this I want to change default order of search result by multiple option where one is price and other is Yes/No type custom attribute. Sep 12 '16 at 11:29
  • Sorry, I already realized this after re-reading and retracted my close vote. This will need custom coding. I'll come back to the question if I find some time for it. Sep 12 '16 at 12:06
  • Calling "addAttributeToSort" twice should work. What is the result of this combination? And what's the SQL query? You can find out by $collection->getSelect()->__toString() Sep 12 '16 at 13:57
  • Hello @fschmengler, please check my edited question. result of this combination is product list sorted only by special attribute. Sep 13 '16 at 5:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.