1

How can I sort catalogsearch results by 2 attributes instead of "position" or "relevance" only? Not in toolbar, need to sort by default .. for example, first sort products with "attribute1" and then sort with "attribute2" without changing the toolbar and select any of them?

for more info, I have 4 products:

  1. 121/24R
  2. 123/83C
  3. 123/15H
  4. 121/55H

I have 2 attributes that first is for 123,121 and another one is for 24,83,15,55

Now I want to sort products results with first attribute in 'asc' and another one in 'desc'?

like this :

  1. 121/55H
  2. 121/24R
  3. 123/83H
  4. 123/15C

How can I use this method ?

4

First, create an extension (in my example it's called Emzee_Sorter) and listen to the event catalog_block_product_list_collection in your config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <Emzee_Sorter>
            <version>1.0.0</version>
        </Emzee_Sorter>
    </modules>
    <global>
        <models>
            <emzee_sorter>
                <class>Emzee_Sorter_Model</class>
            </emzee_sorter>
        </models>
        <events>
            <catalog_block_product_list_collection>
                <observers>
                    <emzee_sorter>
                        <class>emzee_sorter/observer</class>
                        <method>catalogBlockProductListCollection</method>
                    </emzee_sorter>
                </observers>
            </catalog_block_product_list_collection>
        </events>
    </global>
</config>

Then create the observer class:

<?php

class Emzee_Sorter_Model_Observer
{
    /**
     * @param Varien_Event_Observer $observer
     * @return Emzee_Sorter_Model_Observer
     */
    public function catalogBlockProductListCollection(Varien_Event_Observer $observer)
    {
        $collection = $observer->getEvent()->getCollection();
        $collection->addOrder('sort_2', Varien_Data_Collection::SORT_ORDER_DESC);
        return $this;
    }
}

sort_2 is the attribute containing 55H and so on. Make sure that you set Used in Product Listing to Yes for this attribute so that the attribute can be found when flat catalog is enabled.

This code will sort the products on search results and category pages. If you want to limit the functionality to search results, you can do something like this:

<?php

class Emzee_Sorter_Model_Observer
{
    /**
     * @param Varien_Event_Observer $observer
     * @return Emzee_Sorter_Model_Observer
     */
    public function catalogBlockProductListCollection(Varien_Event_Observer $observer)
    {
        $request = Mage::app()->getFrontController()->getRequest();
        $action = $request->getModuleName() . '_' . $request->getControllerName() . '_' . $request->getActionName();
        if ($action === 'catalogsearch_result_index' || $action === 'catalogsearch_advanced_result') {
            $collection = $observer->getEvent()->getCollection();
            $collection->addOrder('sort_2', Varien_Data_Collection::SORT_ORDER_DESC);
        }
        return $this;
    }
}
|improve this answer|||||
  • not worked, maybe somethings was wrong, can you create extension and send me download link here? – sIiiS Nov 9 '13 at 14:03
  • I created 2 attributes (text fields): the first attribute, "sort_1", contains 121 or 123. The second attribute "sort_2" contains 55H, 24R and so on. I created 4 products, entered the attribute values, set both attributes to be used in product listing and the first attribute to be available in the sort dropdown. I really created all of this to verify before answering and it worked in an CE 1.8 store. Please recheck. – Matthias Zeis Nov 9 '13 at 14:13
  • Source code is here: gist.github.com/mzeis/7385851 – Matthias Zeis Nov 9 '13 at 14:17
  • I'm glad it works for you now. – Matthias Zeis Nov 9 '13 at 20:47

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.