<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>About Web Development by Activo &#187; prepareMassaction()</title>
	<atom:link href="http://www.activoinc.com/blog/tag/preparemassaction/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.activoinc.com/blog</link>
	<description>Web Development in a Web 2.0 World</description>
	<lastBuildDate>Wed, 24 Nov 2010 00:25:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Magento&#8217;s massAction for Custom Plugins</title>
		<link>http://www.activoinc.com/blog/2009/10/08/magentos-massaction-for-custom-plugins/</link>
		<comments>http://www.activoinc.com/blog/2009/10/08/magentos-massaction-for-custom-plugins/#comments</comments>
		<pubDate>Thu, 08 Oct 2009 14:00:43 +0000</pubDate>
		<dc:creator>Ron Peled</dc:creator>
				<category><![CDATA[Magento]]></category>
		<category><![CDATA[massAction]]></category>
		<category><![CDATA[prepareMassaction()]]></category>

		<guid isPermaLink="false">http://www.activoinc.com/blog/?p=354</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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:</p>
<p><img class="aligncenter size-full wp-image-355" title="magento-mass-action" src="http://www.activoinc.com/blog/wp-content/uploads/2009/10/magento-mass-action.jpg" alt="magento-mass-action" width="453" height="209" /></p>
<p><strong>1. Prepare a Grid Container and Grid in your backend:<br />
</strong>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 <a href="http://t.wits.sg/"><em><strong>Tips for Twits</strong></em></a> Magento Blog, the article is called <em><strong><a title="Permanent Link to Howto: Repackageable custom extension development in Magento" rel="bookmark" href="http://t.wits.sg/2009/03/31/howto-repackageable-custom-extension-development-in-magento/">Howto: Repackageable custom extension development in Magento</a>.</strong></em></p>
<p><strong>2. Add the _prepareMassaction() method inside the Grid class:</strong><em><strong><br />
</strong></em>Add the following code inside your module class that extends Mage_Adminhtml_Block_Widget_Grid:</p>
<pre>protected function _prepareMassaction()
 {
 $this-&gt;setMassactionIdField('entity_id');
 $this-&gt;getMassactionBlock()-&gt;setFormFieldName('product');

 $this-&gt;getMassactionBlock()-&gt;addItem('add', array(
 'label'    =&gt; Mage::helper('rma')-&gt;__('Add Products to <a class="zem_slink" title="Return merchandise authorization" rel="wikipedia" href="http://en.wikipedia.org/wiki/Return_merchandise_authorization">RMA</a> List'),
 'url'      =&gt; $this-&gt;getUrl('*/*/massAdd'),
 ));

 return $this;
 }</pre>
<p>A few things to notice:</p>
<p>2.1. &#8216;entity_id&#8217; is the database column that serves as the unique identifier throughout your data structure, including: db table, single product magento model, and the collection.<br />
2.2. setFormFieldName(&#8217;product&#8217;) &#8211; the text &#8216;product&#8217; is flexible but you will need it in the next step, so remember it.<br />
2.3. The routing string &#8216;*/*/massAdd&#8217; will triger a method called massAddAction() in your controller.</p>
<p><strong>3. Prepare the actual action to be taken in your controller</strong><br />
Here is the code that needs to be added, notes are below:</p>
<pre>public function massAddAction()
 {
 $productIds = $this-&gt;getRequest()-&gt;getParam('product');
 if(!is_array($productIds)) {
 Mage::getSingleton('adminhtml/session')-&gt;addError(Mage::helper('rma')-&gt;__('Please select product(s)'));
 } else {
 try {
 $product = Mage::getModel('catalog/product');

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

 $this-&gt;_redirect('*/*/index');

 }</pre>
<p>Notes:</p>
<p>3.1. The method name is routed from the previously provided $this-&gt;getUrl(&#8217;*/*/massAdd&#8217;) method.<br />
3.2. Yes, the variable <strong>$productIds </strong>contains a simple array of entity_id numbers. This is not a collection by any means, so you should treat it this way.<br />
3.3. Inside the main foreach loop, you can pretty much do whatever action you wanted.</p>
<p>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&#8217; lives a bit easier.</p>
<p>Let me know how what other advanced Magento features you use the most.</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><span class="zem-script more-related pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div>
<h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li>October 22, 2009 -- <a href="http://www.activoinc.com/blog/2009/10/22/how-to-detect-if-the-page-is-secure-in-a-magento-template/" title="How to Detect if the Page is Secure in a Magento Template?">How to Detect if the Page is Secure in a Magento Template?</a> (0)</li><li>September 29, 2009 -- <a href="http://www.activoinc.com/blog/2009/09/29/magentos-order-management-workflow-comprehensive-but-unrealistic-2/" title="Magento&#8217;s Order Management Workflow: Comprehensive but Unrealistic #2">Magento&#8217;s Order Management Workflow: Comprehensive but Unrealistic #2</a> (2)</li><li>June 18, 2009 -- <a href="http://www.activoinc.com/blog/2009/06/18/the-case-for-zencart-supporting-the-long-tail-of-ecommerce/" title="The Case for ZenCart: Supporting the Long Tail of eCommerce">The Case for ZenCart: Supporting the Long Tail of eCommerce</a> (2)</li><li>January 28, 2009 -- <a href="http://www.activoinc.com/blog/2009/01/28/open-source-ecommerce-the-good-the-bad-and-the-ugly/" title="Open Source eCommerce: the Good, the Bad, and the Ugly!">Open Source eCommerce: the Good, the Bad, and the Ugly!</a> (1)</li><li>December 7, 2008 -- <a href="http://www.activoinc.com/blog/2008/12/07/a-list-of-cms-ecommerce-and-blogging-systems-that-officially-support-jquery/" title="A list of CMS and eCommerce systems that officially support jQuery">A list of CMS and eCommerce systems that officially support jQuery</a> (2)</li><li>December 4, 2008 -- <a href="http://www.activoinc.com/blog/2008/12/04/can-magento-and-typo3-be-integrated-yes-with-typogento/" title="Can Magento and Typo3 be integrated? Yes, with TypoGento">Can Magento and Typo3 be integrated? Yes, with TypoGento</a> (0)</li><li>October 29, 2008 -- <a href="http://www.activoinc.com/blog/2008/10/29/is-magento-commerce-the-new-joomla/" title="Is Magento Commerce the new Joomla?">Is Magento Commerce the new Joomla?</a> (1)</li><li>September 22, 2008 -- <a href="http://www.activoinc.com/blog/2008/09/22/zencart-and-magento-for-ecommerce/" title="ZenCart and Magento for eCommerce">ZenCart and Magento for eCommerce</a> (2)</li><li>August 25, 2008 -- <a href="http://www.activoinc.com/blog/2008/08/25/zencart-ver-1-4-looking-ahead/" title="Looking Ahead: ZenCart ver 1.4">Looking Ahead: ZenCart ver 1.4</a> (3)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.activoinc.com/blog/2009/10/08/magentos-massaction-for-custom-plugins/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

