Magento发票网格filter_condition_callback无效

时间:2015-08-26 09:04:47

标签: magento filter grid

我使用观察者向发票网格添加了一个自定义列。 问题是我不能按新列排序或过滤。

我添加了一个过滤条件回调,但没有调用该函数。

这是我的Observer.php

class DB_CustomGrid_Model_Adminhtml_Observer
{
    public function onBlockHtmlBefore(Varien_Event_Observer $observer)
    {
        $block = $observer->getBlock();

        $payment_methods = array();
        $readConnection = Mage::getSingleton('core/resource')->getConnection('core_read');
        $query = 'SELECT method FROM '.Mage::getSingleton('core/resource')->getTableName('sales/order_payment').' GROUP BY method';
        $methods = $readConnection->fetchAll($query);
        foreach($methods as $payment) {
            if($payment["method"] !== 'free') {
                $payment_methods[$payment["method"]] = Mage::getStoreConfig('payment/'.$payment["method"].'/title');
            }
        }

        switch ($block->getType()) {
            case 'adminhtml/sales_invoice_grid':
                $block->addColumnAfter('state', array(
                    'header' => Mage::helper('sales')->__('Payment Method'),
                    'index' => 'method',
                    'type'  => 'options',
                    'width' => '70px',
                    'options' => $payment_methods,
                    'filter' => false,
                    'filter_condition_callback' => array($this, '_myCustomFilter'),
                ), 'method');
            break;
        }
    }

    public function beforeCollectionLoad(Varien_Event_Observer $observer)
    {
        $collection = $observer->getOrderInvoiceGridCollection();
        $collection->join(array('payment'=>'sales/order_payment'),'main_table.order_id=parent_id',array('method'));
    }

    protected function _myCustomFilter($collection, $column)
    {
        exit;
        if (!$value = $column->getFilter()->getValue()) {
            return $collection;
        }

        $collection->getCollection()->getSelect()->where("sales_order_payment.method like ?", "%$value%");
        return $collection;
    }
}

我添加了退出;检查函数是否被调用。

1 个答案:

答案 0 :(得分:2)

试试这个:

向观察者类添加一个新的受保护函数:

protected function _callProtectedMethod($object, $methodName) {
    $reflection = new ReflectionClass($object);
    $method = $reflection->getMethod($methodName);
    $method->setAccessible(true);
    return $method->invoke($object);
}

然后在$block->sortColumnsByOrder()

之后直接致电$this->_callProtectedMethod($block, '_prepareCollection')和新功能$block->addColumnAfter();