Please help me I want to see the products in Search Results order by ordered quantity means which products are more sold those products shows at first Thanks in

  • which magento version are you using?
    – Aboodred1
    Apr 28, 2014 at 21:37
  • Magento ver. am using enterprise edition
    – latha
    Apr 29, 2014 at 5:46
  • Actually i added custom code in Layer.php in public function prepareProductCollection($collection) added these few lines $collection->getSelect() ->joinLeft( 'sales_flat_order_item AS order_item', 'e.entity_id = order_item.product_id', 'SUM(order_item.qty_ordered) AS ordered_qty') ->group('e.entity_id') ->order('qty_ordered DESC'); now i got the products list ordere by ordered quantity but here have 2 problems 1 is pagination and result count shows as 1 every result.. Please help me....
    – latha
    Apr 29, 2014 at 7:19

1 Answer 1


One way of solving your issue is; by adding additional sort option like Relevance/Position sort option. In order for you to add a "ordered quantity" option you need to do the following:

  • First copy Config.php from app/code/core/Mage/Catalog/Model/Config.php to app/code/local/Mage/Catalog/Model/Config.php and replace this function getAttributeUsedForSortByArray() with the following code:

public function getAttributeUsedForSortByArray()
    $options = array(
        'position'  => Mage::helper('catalog')->__('Position'),
        'ordered_qty'  => Mage::helper('catalog')->__('Ordered Qty'),
    foreach ($this->getAttributesUsedForSortBy() as $attribute) {
        /* @var $attribute Mage_Eav_Model_Entity_Attribute_Abstract */
        $options[$attribute->getAttributeCode()] = $attribute->getStoreLabel();

    return $options;

  • Second copy Toolbar.php from app/code/core/Mage/Catalog/Block/Product/List/Toolbar.php to app/code/local/Mage/Catalog/Block/Product/List/Toolbar.php and replace this function setCollection() with the following code:

public function setCollection($collection)
    $this->_collection = $collection;


    // we need to set pagination only if passed value integer and more that 0
    $limit = (int)$this->getLimit();
    if ($limit) {
    if ($this->getCurrentOrder()) {
        if($this->getCurrentOrder() == 'ordered_qty') {
                 ->joinLeft('sales_flat_order_item AS so', 'e.entity_id = so.product_id', 'SUM(so.qty_ordered) AS ordered_qty')
                 ->group('e.entity_id')->order('ordered_qty ' . $this->getCurrentDirection());
        } else {
            $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
    return $this;

  • Third to set "Ordered Qty" sort option as default for Search Result and Advance Search Result add/edit local.xml under your active theme folder app/design/frontend/your_package/your_theme/layout/local.xml and add the following:

<?xml version="1.0" encoding="UTF-8"?>
        <reference name="search_result_list">
            <action method="setDefaultDirection"><param>desc</param></action>
            <action method="setSortBy"><param>ordered_qty</param></action>
        <reference name="search_result_list">
            <action method="setDefaultDirection"><param>desc</param></action>
            <action method="setSortBy"><param>ordered_qty</param></action>

I hope this will help you.

  • Hi..Thanks for your reply... i don't want to show..orders list at front end ..in my app that front option is not there.... I want to enable that order in search result page, got that page path i.e app/code/core/Mage/catalogsearch/model/Layer.php Pls help me.....
    – latha
    Apr 30, 2014 at 17:53
  • @latha with my solution, hiding ordered qty option from frontend is considered an option for you?
    – Aboodred1
    Apr 30, 2014 at 18:05
  • ok...thanq.. but what i done Layer.php is it not correct...
    – latha
    Apr 30, 2014 at 18:12
  • thanq...so much.... It's very nice... you save my time... Thanq very much... I done with only second step...it's working...nice answer..it's save my time...
    – latha
    May 1, 2014 at 7:40
  • @latha glad it worked for you :D ... feel free to market it as correct answer if you will
    – Aboodred1
    May 1, 2014 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.