Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento Custom Sort Option

How do I add custom sort option in Magento. I want to add Best Sellers, Top rated and exclusive in addition to sort by Price. Please help

like image 767
Anuj Sharma Avatar asked Jan 13 '23 09:01

Anuj Sharma


1 Answers

For Best Sellers

haneged in code/local/Mage/Catalog/Block/Product/List/Toolbar.php method setCollection to

public function setCollection($collection) {
    parent::setCollection($collection);
    if ($this->getCurrentOrder()) {
        if($this->getCurrentOrder() == 'saleability') {
            $this->getCollection()->getSelect()
                 ->joinLeft('sales_flat_order_item AS sfoi', 'e.entity_id = sfoi.product_id', 'SUM(sfoi.qty_ordered) AS ordered_qty')
                 ->group('e.entity_id')->order('ordered_qty' . $this->getCurrentDirectionReverse());
        } else {
            $this->getCollection()
                 ->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
        }
    }

    return $this;
}

After setCollection I added this method:

public function getCurrentDirectionReverse() {
    if ($this->getCurrentDirection() == 'asc') {
        return 'desc';
    } elseif ($this->getCurrentDirection() == 'desc') {
        return 'asc';
    } else {
        return $this->getCurrentDirection();
    }
}

And finally I changed mehod setDefaultOrder to

public function setDefaultOrder($field) {
    if (isset($this->_availableOrder[$field])) {
        $this->_availableOrder = array(
            'name'        => $this->__('Name'),
            'price'       => $this->__('Price'),
            'position'    => $this->__('Position'),
            'saleability' => $this->__('Saleability'),
        );
        $this->_orderField = $field;
    }

    return $this;
}

for Top rated

http://www.fontis.com.au/blog/magento/sort-products-rating

try above code.

for date added

Magento - Sort by Date Added

i am not associate with any of the above link for any work or concern it is just for knowledge purpose and to solve your issue.

hope this will sure help you.

like image 118
liyakat Avatar answered Jan 25 '23 07:01

liyakat