0

When performing a search I am wanting to show the results in order of the categories.

I am aware that magento does not allow this natively as products may be in more than one category, however in this example the products will belong to either one or the other category.

To clarify, I have only two categories containing complete products, and component products - e.g. rc helicopter, and rc helicopter components to repair or whatever.

I want on the search results for the complete products to be listed first.

On my products/list.phtml file I have added:

$_productCollection
    ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
    ->addAttributeToFilter('category_id', 'ASC');

but this does not appear to work.

Is there a way to do this in the local.xml file? Should I create an observer for the search and add the category order-by there (if so, how should I do that)?

Edit

I have also added the following to my local.xml file but also does not work:

<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>
0

Had to override the core version of CatalogSearch/Block/Result.php (copy app/code/core/Mage/CatalogSearch/Block/Result.php into local) and alter the function setListOrders():

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

    $this->getListBlock()
        ->setAvailableOrders($availableOrders)
        ->setDefaultDirection('desc')
        ->setSortBy('category'); //this line

    return $this;
}

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.