Directory Image
This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Privacy Policy.

How to Filter the Order Grid on Magento for Multiple IDs?

Author: Deepa Ranganathan
by Deepa Ranganathan
Posted: May 07, 2015

If you want to display data in an efficient manner, then you don't have any other alternative except a grid, when working with Magento. There are many ways of displaying data on the store, and each of these requests can be perfectly handled by grids. If you want to filter order grid using multiple IDs, to track the multiple orders, then order grid in Magento would be the place to begin.

How to Filter Order Grid for Multiple IDs?

You can use the same hack across the different order grids. To begin with, you will need to rewrite your order grid block located in the path app\code\core\Mage\Adminhtml\Block\Sales\Order\Grid.php

Use the code below to rewrite the order block. The _prepareColumns() method is where you will need to add an element within the array.

protected function _prepareColumns()

{

$this->addColumn('real_order_id', array(

'header'=> Mage::helper('sales')->__('Order #'),

'width' => '250px',

'type' => 'text',

'index' => 'increment_id',

'filter_condition_callback' => array($this, 'spaceSeparatedFilter')//calling spaceSeparatedFilter method

));

....

}

A particular row within the grid, when being rendered, the renderers within the array are called. These renderers are also passed through the cell that is being rendered as a parameter.

On the other hand filter callbacks are called along with the entire column and collection as a parameter. On the other hand, you can create your own method and render it using filter_condition_callback function. You can even add custom queries in the database, and capture input using the grid.

The filter callback will call the spaceSeparatedFilter() method and pass the real_order_id parameter through it as an entire column. You will need to declare the method being used within the grid.php file in order to render it usable

protected function spaceSeparatedFilter($collection, $column)

{

if (!$value = $column->getFilter()->getValue()) {

return $this;

}

//if there was a space input

else if(preg_match('/\s+/', $value))

{

//explode by space, getting array of IDs

$val = explode(" ", $value);

//filter the collection, where collection index (order_id) is present in $val array

$this->getCollection()->addAttributeToFilter($column->getData('index'), array('in'=>$val));

}

else

{

//else use default grid filter functionality (like $value input)

$this->getCollection()->addAttributeToFilter($column->getData('index'), array('like' => '%'.$value.'%'));

}

return $this;

}

Using these two codes you can easily filter the orders using multiple order Ids. You will have to separate the Ids using a space else it will work in the default manner which is using the query method.

Conclusion:

Other features like filtering, sorting and exporting can also be used in a similar fashion. This method will help you filter and track and monitor the orders for multiple customers.

Note:

Don't forget to have a backup of your store before you start coding

Deepa, a technical writer with Semaphore Software, who now devotes her time in advising its clients to hire dedicated Magento developers. Her love for reading helps her constantly provide latest information on different technical and design aspects of Magento.

About the Author

Deepa is a technical writer with Semaphore Software.

Rate this Article
Leave a Comment
Author Thumbnail
I Agree:
Comment 
Pictures
Author: Deepa Ranganathan

Deepa Ranganathan

Member since: Mar 29, 2015
Published articles: 42

Related Articles