Adding New Mass Action to Admin Grid in Magento: Getting Order ID in Controller
If you’re trying to add a new mass action to the admin grid for Sales > Orders in Magento, and are having trouble retrieving the order ID in your controller, this post will guide you through the process.
Understanding the Problem
The issue arises when attempting to get the order_id parameter from the request object using $this->getRequest()->getParam('order_id'). This fails because the mass action parameters aren’t automatically set by Magento’s core framework.
Setting the Mass Action Parameters
To make this work, you need to manually set the order IDs in your grid collection. Here’s how:
- In your
Grid.phpfile (usually located atapp/code/local/YourVendor/YourModule/Block/Adminhtml/Sales/Order/Grid.php), override the_prepareCollection()method.
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->addFieldToFilter('status', array('neq' => array('processing', 'pending')));
// Here's where you set the order IDs for mass action
if ($ids = $this->getRequest()->getParam('massaction')) {
$collection->addFieldToFilter('entity_id', array('in' => $ids));
}
return parent::_prepareCollection();
}
- Then, in your
Controller.phpfile (usually located atapp/code/local/YourVendor/YourModule/controllers/Adminhtml/Sales/OrderController.php), override the_initOrder()method.
protected function _initOrder()
{
$id = $this->getRequest()->getParam('order_id'); // This should now work
// Rest of your code...
}
Processing the Mass Action
Now, let’s modify your mass action controller to process multiple orders:
- In your
IndexController.phpfile (usually located atapp/code/local/YourVendor/YourModule/controllers/Adminhtml/Sales/Order/IndexController.php), create a new method for your custom action.
public function yourAction()
{
$ids = $this->getRequest()->getParam('massaction');
if (!is_array($ids)) {
return;
}
try {
foreach ($ids as $id) {
// Assuming you want to change the order status to 'processing'
$order = Mage::getModel('sales/order')->load($id);
$order->setStatus('processing');
$order->save();
}
$this->_getSession()->addSuccess(Mage::helper('adminhtml')->__('Selected orders have been processed'));
} catch (Exception $e) {
$this->_getSession()->addError($e->getMessage());
}
// Redirect to the grid
$this->_redirect('*/*/');
}
Don’t forget to add this new action to your config file at app/code/local/YourVendor/YourModule/etc/config.xml:
<config>
<global>
<adminhtml>
<controllers>
<sales_order_index>
<rewrite>
<your_action>YourVendor_YourModule_Sales_Order_IndexController_yourAction</your_action>
</rewrite>
</sales_order_index>
</controllers>
</adminhtml>
</global>
</config>
Conclusion
As you can see, to get the order IDs for a mass action in Magento, you need to manually set them in your grid collection and then retrieve them in your controller. This approach allows you to process multiple orders based on the selected IDs.
Remember to replace YourVendor, YourModule, and yourAction with your actual vendor name, module name, and desired action name respectively.
For more advanced customizations like adding a form for additional data entry before processing, check out this guide from Laravel Company.