Magento 1.9 – How to Add Searchable Column ‘Shipping Methods’ to Sales/Orders Admin Grid

columnmagento-1.9order-gridsales-order

To make my admin Sales/Orders grid better I added column to the grid:
enter image description here

and as on the picture above it shows order delivery method but …
how to create "delivery method" dropdown list to make it easier to use?
So far I tried to add a class:

public function getCarrierList()
{
    $shippingMethods = Mage::getSingleton('shipping/config')->getActiveCarriers();  
    $methods = array();
    foreach($shippingMethods as $method)
    {
            $methods[$method['value']] = $methods[$method['label']];
    }
    return $methods;
}

and added to the column definition:

    $this->addColumn('shipping_method', array(
        'header' => Mage::helper('sales')->__('Shipping Method'),
        'index' => 'shipping_description',
        'type'=> 'options',
        'filter_index' => 'shipping_method',
        'options' => $this->getCarrierList(),
    ));

but then my column stays empty and drop-down is also empty.
Of course I can live without it but if I can have a list instead of a typing method name .. would be nice.

Best Answer

Please try the below code to show shipping method in sales grid.

protected function _prepareCollection()
    {
        $collection = Mage::getResourceModel($this->_getCollectionClass());

        $collection->getSelect()->join( array('order'=> 'sales_flat_order'), 'order.entity_id = main_table.entity_id', array('order.shipping_method'));

        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

In _prepareColumns function,

$this->addColumn('shipping_method', array(
        'header' => Mage::helper('sales')->__('Shipping Method'),
        'index' => 'shipping_method',
        'type'=> 'options',
        'options' => $this->getCarrierList(),
    ));

The carrierList function,

 public function getCarrierList()
    {
        $shippingMethods = Mage::getSingleton('shipping/config')->getActiveCarriers(); 
        $methods = array();
        foreach($shippingMethods as $_code => $_method)
        {
             $_title = Mage::getStoreConfig("carriers/$_code/title");
           if(!empty($_title)) {
             $_title = $_code; 
             $methods[$_title ."_$_code"]=  $_title ."_$_code";
            }
        }
        return $methods;
    }