user3684098
user3684098

Reputation: 389

Magento invoice grid filter_condition_callback not working

I added a custom column to invoice grid using an observer. The problem is that I can't sort or filter by the new column.

I added a filter condition callback but the function is not called.

Here is my 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;
    }
}

I added an exit; to check if the function is called or not.

Upvotes: 2

Views: 1662

Answers (1)

codiax
codiax

Reputation: 36

Try this:

Add a new protected function to your observer class:

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

Then call $block->sortColumnsByOrder() and the new function $this->_callProtectedMethod($block, '_prepareCollection') directly after $block->addColumnAfter();

Upvotes: 2

Related Questions