Filter order grid by multiple ID in magento

Filter order grid by multiple ID in magento

In this poet we will show you how to create Filter order grid by multiple ID in magento. We all know how nice Magento Filter order grid may be – there’s no different once it involves displaying data in an exceedingly economical manner.

Our purchasers typically (almost always) have specific necessities once it involves Magento. one in every of them had letter of invitation to be ready to filter the order grid by multiple order ID’s.

This may be helpful if you’ve got variety of orders you need to trace – filtering them one by one can be tedious and long task.

The Trickery for Filter order grid by multiple ID in magento

The trick i am close to show you’ll be worn out virtually any grid, once you recognize however it’s done.

Firstly, rewrite your order grid block (app\code\core\Mage\Adminhtml\Block\Sales\Order\Grid.php) – I presume you will know how to try to to this.

In this method _prepareColumns() , add array element to shown as below (at line 8)

protected function _prepareColumns()
{
	$this->addColumn('real_order_id', array(
	    // set header
		'header'=> Mage::helper('sales')->__('Order #'),
		// set type
		'type'  => 'text',
		// set index
		'index' => 'increment_id',
		// set width
		'width' => '217px',
		// set method for calling spaceSeparatedFilter method
		'filter_condition_callback' => array($this, 'spaceSeparatedFilter')		
	));
// your data ....
}

Renderers square measure being referred to as once a row is being rendered in our grid, and find passed the cell that’s being rendered as a parameter.

Filter callbacks, on the opposite hand, get referred to as with the complete column and assortment as parameters. As a result, we are able to produce our own methodology, decision it via filter_condition_callback component, and do custom queries to the info, catch the input from our grid, etc.

This is precisely what we’ll do.

Our filter asking can decision the spaceSeparatedFilter() methodology and pass it the real_order_id column as a parameter. Let’s declare our methodology (inside a similar Grid.php file

protected function spaceSeparatedFilter($collection, $column)
{
	if (!$value = $column->getFilter()->getValue()) {
		return $this;
	}
	// if $value was a space input
	else if(preg_match('/\s+/', $value))
	{
		// explode with space and IDs from getting array 
		$get_val = explode(" ", $value);
		// filter the collection, where collection index (order_id) is present in $get_val array
		$this->getCollection()->addAttributeToFilter($column->getData('index'), array('in'=>$get_val));
	}
	else
	{
		// filter functionality (like $value input) for else use default grid 
		$this->getCollection()->addAttributeToFilter($column->getData('index'), array('like' => '%'.$value.'%'));
	}
	return $this;
}

These two pieces of code enable you to filter orders by multiple order id’s, as long as you separate them with a space.

If we have not entered a space, it will work as default (example : filtering orders usind an ID using your query of 'LIKE' ).

we can also use all features of the grid: export, sorting, filtering, without any problems.

We hope this will be useful to Filter order grid by multiple ID in magento.

Leave a Comment

Your email address will not be published. Required fields are marked *

62  +    =  65

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US