Posts Tagged ‘massAction’

Magento’s massAction for Custom Plugins

October 8th, 2009
Comments Off

Ever wanted to allow your users to make mass action on your Magento Custom Plugin records? The good news is that it is built in to the Magento Admin Panel and is available at the community edition. The bad news is that there is absolutely no documentation on how to use it. Here are the 3 steps you will need to take in order to develop this feature in your custom plugin:


1. Prepare a Grid Container and Grid in your backend:
For this you will need a collection of records, a controller, and the basic blocks of a grid admin page. I will not cover this in this post, but you can find more about setting it up nicely on Tips for Twits Magento Blog, the article is called Howto: Repackageable custom extension development in Magento.

2. Add the _prepareMassaction() method inside the Grid class:
Add the following code inside your module class that extends Mage_Adminhtml_Block_Widget_Grid:

protected function _prepareMassaction()

 $this->getMassactionBlock()->addItem('add', array(
 'label'    => Mage::helper('rma')->__('Add Products to RMA List'),
 'url'      => $this->getUrl('*/*/massAdd'),

 return $this;

A few things to notice:

2.1. ‘entity_id’ is the database column that serves as the unique identifier throughout your data structure, including: db table, single product magento model, and the collection.
2.2. setFormFieldName(‘product’) – the text ‘product’ is flexible but you will need it in the next step, so remember it.
2.3. The routing string ‘*/*/massAdd’ will triger a method called massAddAction() in your controller.

3. Prepare the actual action to be taken in your controller
Here is the code that needs to be added, notes are below:

public function massAddAction()
 $productIds = $this->getRequest()->getParam('product');
 if(!is_array($productIds)) {
 Mage::getSingleton('adminhtml/session')->addError(Mage::helper('rma')->__('Please select product(s)'));
 } else {
 try {
 $product = Mage::getModel('catalog/product');

 foreach ($productIds as $productId) 
 $rmaProduct = Mage::getModel('rma/product');
 'Total of %d product(s) were successfully added to the RMA product list', count($productIds)
 } catch (Exception $e) {




3.1. The method name is routed from the previously provided $this->getUrl(‘*/*/massAdd’) method.
3.2. Yes, the variable $productIds contains a simple array of entity_id numbers. This is not a collection by any means, so you should treat it this way.
3.3. Inside the main foreach loop, you can pretty much do whatever action you wanted.

In conclusion, with about 20 lines of code and updates to two files we are able to leverage a powerful built in feature provided by Magento which allows for mass actions on our records. Obviously, this little excerpt is not for the beginners out there. However, I hope this will make some developers’ lives a bit easier.

Let me know how what other advanced Magento features you use the most.

Magento , ,