Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento Add Payment Method to Admin Order Grid

Tags:

magento

I am trying to add a custom column to the Sales Order grid and I have added the join and column to the collection, but the column shows empty. Also, when I try to filter, I get this error: Column not found: 1054 Unknown column 'method' in 'where clause'. My code seems to be the same as in the majority of tutorials out there, so I can't figure out why my column is devoid of any payment data. I echoed the final SQL query and ran it in MySQL Workbench and the results and syntax seem to be fine. Am I missing something? Do I need to add a column to the sales_flat_order_grid table? I realize that my "pretty" payment method names may not mesh well with the payment code I am displaying in the column when I go to filter, but I have the same issues when using payment code in the filter options. I figured I would hit that detail later, after I get some column data. I am using Enterprise 1.12.0.2.

app/code/local/mymodule/adminhtml/block/sales/order/grid:

     protected function _prepareCollection()
{
    $collection = Mage::getResourceModel($this->_getCollectionClass());
    $collection->getSelect()->joinLeft(array('sfop'=>'sales_flat_order_payment'), 'main_table.entity_id = sfop.parent_id',array('sfop.method'));
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

     protected function _prepareColumns()
{  //edited for brevity...only my additions below![enter image description here][1]
         $payments = Mage::getSingleton('payment/config')->getActiveMethods();
    $methods = array();
    foreach ($payments as $paymentCode=>$paymentModel)
    {
        $paymentTitle = Mage::getStoreConfig('payment/'.$paymentCode.'/title');
        $methods[$paymentCode] = $paymentTitle;
    }

    $this->addColumn('method', array(
        'header' => Mage::helper('sales')->__('Payment Method'),
        'index' => 'method',
        'filter_index' => 'sfop.method',
        'type'  => 'options',
        'width' => '70px',
        'options' => $methods,
    ));

}

Admin Screen

like image 579
Stephanie Ransdell Avatar asked Jul 26 '13 17:07

Stephanie Ransdell


1 Answers

You need to return the parent of the Admin Grid not the parent of the class you are rewriting.

Replace the method _prepareCollection() with the following:-

protected function _prepareCollection()
{
    $collection = Mage::getResourceModel($this->_getCollectionClass());
    $collection->join(array('payment'=>'sales/order_payment'),'main_table.entity_id=parent_id','method');
    $this->setCollection($collection);
    return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
}
like image 79
Meabed Avatar answered Sep 21 '22 16:09

Meabed