Sep 27, 2012

Magento - How to create multiple fields to sort?

Find the class and made modification on the class.
Mage_Catalog_Block_Product_List_Toolbar

Find the getAvailableOrders() and made some modification.
     public function getAvailableOrders()
    {
        $orders = array(
            'news_from_date,entity_id'         => 'Newest',
            'name,brand'              => 'Brand Name',
            'rating'        => 'Top Rated',
        );

        $this->_availableOrder = $orders;
        
        return $orders;
    }

Also try to look setCollection($collection)
    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()) {
            // parse the attribute
            $orders = explode(',', $this->getCurrentOrder());
            foreach($orders as $order){
                // Add the other statement for to sort
                $this->_collection->setOrder($order, $this->getCurrentDirection());
            }
        return $this;
    }

Thanks just simple code to made multiple sorting. But i not suggest you to modify the magento core as is. if you can able to override class it better.

class Globe_Catalog_Block_Product_List_Toolbar 
  extends Mage_Catalog_Block_Product_List_Toolbar{

 // Your method you want here and method you want to override, some example.

    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()) {
            // parse the attribute
            $orders = explode(',', $this->getCurrentOrder());
            foreach($orders as $order){
                // Add the other statement for to sort
                $this->_collection->setOrder($order, $this->getCurrentDirection());
            }
        return $this;
    }
  

   // ... You other method.
}

No comments: