MyPage is a personalized page based on your interests.The page is customized to help you to find content that matters you the most.


I'm not curious

How to Filter the Order Grid on Magento for Multiple IDs

Published on 07 May 15
1207
0
2
How to Filter the Order Grid on Magento for Multiple IDs - Image 1
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




















































How to Filter the Order Grid on Magento for Multiple IDs - Image 1

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

Related Posts:
Post a Comment

Please notify me the replies via email.

Important:
  • We hope the conversations that take place on MyTechLogy.com will be constructive and thought-provoking.
  • To ensure the quality of the discussion, our moderators may review/edit the comments for clarity and relevance.
  • Comments that are promotional, mean-spirited, or off-topic may be deleted per the moderators' judgment.
You may also be interested in
Awards & Accolades for MyTechLogy
Winner of
REDHERRING
Top 100 Asia
Finalist at SiTF Awards 2014 under the category Best Social & Community Product
Finalist at HR Vendor of the Year 2015 Awards under the category Best Learning Management System
Finalist at HR Vendor of the Year 2015 Awards under the category Best Talent Management Software
Hidden Image Url

Back to Top