Magento Search Results – Sort by Ordered Quantity

catalogsearchmagento-enterprisesorting

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

Best Answer

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;

    $this->_collection->setCurPage($this->getCurrentPage());

    // we need to set pagination only if passed value integer and more that 0
    $limit = (int)$this->getLimit();
    if ($limit) {
        $this->_collection->setPageSize($limit);
    }
    if ($this->getCurrentOrder()) {
        if($this->getCurrentOrder() == 'ordered_qty') {
            $this->getCollection()->getSelect()
                 ->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"?>
<layout>
    <catalogsearch_result_index>
        <reference name="search_result_list">
            <action method="setDefaultDirection"><param>desc</param></action>
            <action method="setSortBy"><param>ordered_qty</param></action>
        </reference>
    </catalogsearch_result_index>
    <catalogsearch_advanced_result>
        <reference name="search_result_list">
            <action method="setDefaultDirection"><param>desc</param></action>
            <action method="setSortBy"><param>ordered_qty</param></action>
        </reference>
    </catalogsearch_advanced_result>
</layout>

I hope this will help you.

Related Topic