Archive

Posts Tagged ‘prepareMassaction()’

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:

magento-mass-action

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->setMassactionIdField('entity_id');
 $this->getMassactionBlock()->setFormFieldName('product');

 $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) 
 {
 $product->reset()->load($productId);
 $rmaProduct = Mage::getModel('rma/product');
 $rmaProduct->setName($product->getName())
 ->setDescription($product->getDescription())
 ->setStatus(Activo_RMA_Model_Product::STATUS_ENABLED)
 ->setCreatedAt(now())
 ->setCatalogProductId($productId)
 ->save();
 }
 Mage::getSingleton('adminhtml/session')->addSuccess(
 Mage::helper('rma')->__(
 'Total of %d product(s) were successfully added to the RMA product list', count($productIds)
 )
 );
 } catch (Exception $e) {
 Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
 }
 }

 $this->_redirect('*/*/index');

 }

Notes:

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 , ,