简体   繁体   中英

Magento Add Payment Method to Admin Order Grid

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,
    ));

}

管理员屏幕

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();
}
$collection->getSelect()->joinLeft('sales_flat_order_payment', 'main_table.entity_id = sales_flat_order_payment.parent_id',array('method'));
$this->addColumn('method', array(
        'header' => Mage::helper('sales')->__('Method'),
        'index' => 'method',
        'filter_index' => 'sales_flat_order_payment.method',
        ));

The solution proposed in:

http://www.atwix.com/magento/column-to-orders-grid/

is much cleaner.

Anyway the way to retrieve the $methods as options for the backend grid is not Multistore-ready if payment methods were enabled on website scope. In this case do:

        $methods = array();
        foreach (Mage::app()->getStores() as $storeId => $store) {
        $payments = Mage::getSingleton('payment/config')->getActiveMethods($storeId);

        foreach ($payments as $paymentCode => $paymentModel) {
            $paymentTitle = Mage::getStoreConfig('payment/'.$paymentCode.'/title');
            if (!isset($methods[$paymentCode])) {
                $methods[$paymentCode] = $paymentTitle;
            }
        }
    }

For the rendering, not all payment methods work with title (TIG PostNL for example)

$payments = Mage::getSingleton('payment/config')->getActiveMethods();
$methods = array();
foreach ($payments as $paymentCode=>$paymentModel)
{
    $paymentTitle = Mage::getStoreConfig('payment/'.$paymentCode.'/title', 1);
    if(empty($paymentTitle)) {
        $paymentTitle = Mage::helper('payment')->getMethodInstance($paymentCode)->getTitle();
    }
    $methods[$paymentCode] = $paymentTitle;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM