title: Magento - Add Custom Mass Action PDF
body: <p><strong>Quick Question (Keep this in mind when reading):</strong></p>
  
  <p>Why is this error being produced (<em>in explanation</em>) and how can I edit the <code>pdfRmaAction()</code> to work correctly (Mass Action Printing)???</p>
  
  <p>**Working in Magento <code>v.1.10.1.0</code> which is the same as <code>v.1.5.1.0</code></p>
  
  <p><strong>Long-winded explanation:</strong></p>
  
  <p>I've downloaded this extension (<a href="http://www.magentocommerce.com/magento-connect/admin-order-printing-extension.html" rel="nofollow">http://www.magentocommerce.com/magento-connect/admin-order-printing-extension.html</a>) to add a button to <strong>each</strong> order so that when you go into the order, you have an extra button to print an RMA (Customized from this extensions Model pdf - turned into a custom RMA form based on invoice)</p>
  
  <p>It works great. However, I want to add Mass Action printing to it so that you can check a few orders and choose <strong>Print RMA</strong> from the dropdown and print the forms for those orders.</p>
  
  <p>In the extensions <code>config.xml</code> file (app/code/local/Nastnet/OrderPrint/etc/), inside the <code>&lt;config&gt;</code> tags is this:</p>
  
  <pre><code>&lt;modules&gt;
      &lt;Nastnet_OrderPrint&gt;
          &lt;version&gt;0.1.3&lt;/version&gt;
      &lt;/Nastnet_OrderPrint&gt;
  &lt;/modules&gt;
  
  &lt;global&gt;
      &lt;blocks&gt;
          &lt;adminhtml&gt;
              &lt;rewrite&gt;
                  &lt;sales_order_grid&gt;Nastnet_OrderPrint_Block_Sales_Order_Grid&lt;/sales_order_grid&gt; &lt;!-- ADDED THIS FOR MASS ACTION PRINTING --&gt;
                  &lt;sales_order_view&gt;Nastnet_OrderPrint_Block_Sales_Order_View&lt;/sales_order_view&gt;
              &lt;/rewrite&gt;
          &lt;/adminhtml&gt;
      &lt;/blocks&gt;
      &lt;rewrite&gt;
          &lt;Nastnet_OrderPrint_OrderController&gt;
              &lt;from&gt;&lt;![CDATA[#/\w+/sales_order/print/#]]&gt;&lt;/from&gt;
              &lt;to&gt;/orderprint/order/print/&lt;/to&gt;
          &lt;/Nastnet_OrderPrint_OrderController&gt;
      &lt;/rewrite&gt;
      &lt;models&gt;
          &lt;Nastnet_OrderPrint&gt;
              &lt;class&gt;Nastnet_OrderPrint_Model&lt;/class&gt;
          &lt;/Nastnet_OrderPrint&gt;
      &lt;/models&gt;
      &lt;pdf&gt;
          &lt;order&gt;
              &lt;default&gt;Nastnet_OrderPrint/order_pdf_items_order_default&lt;/default&gt;
              &lt;grouped&gt;Nastnet_OrderPrint/order_pdf_items_order_grouped&lt;/grouped&gt;
          &lt;/order&gt;
      &lt;/pdf&gt;
  &lt;/global&gt;
  &lt;admin&gt;
      &lt;routers&gt;
          &lt;Nastnet_OrderPrint&gt;
               &lt;use&gt;admin&lt;/use&gt;
              &lt;args&gt;
                  &lt;module&gt;Nastnet_OrderPrint&lt;/module&gt;
                  &lt;!-- This is used when "catching" the rewrite above --&gt;
                  &lt;frontName&gt;orderprint&lt;/frontName&gt;
              &lt;/args&gt;
          &lt;/Nastnet_OrderPrint&gt;
      &lt;/routers&gt;
  &lt;/admin&gt;
  </code></pre>
  
  <p>In (app/code/local/Nastnet/OrderPrint/Block/Sales/Order/) in <code>Grid.php</code> is this:</p>
  
  <pre><code>class Nastnet_OrderPrint_Block_Sales_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid
  {
      protected function _prepareMassaction()
      {
          parent::_prepareMassaction();
  
          // Append new mass action option
          $this-&gt;getMassactionBlock()-&gt;addItem('rmaprint',
          array('label' =&gt; $this-&gt;__('Print RMA'),
                'url'   =&gt; $this-&gt;getUrl('orderprint/order/pdfRma')));
      }
  }
  </code></pre>
  
  <p>This leads to the desired result of inserting "<strong>Print RMA</strong>" into the dropdown menu on the Sales > Orders grid view screen. </p>
  
  <p>In the <code>OrderController.php</code> file (app/code/local/Nastnet/OrderPrint/controllers/) I've added this [copied and edited from the <code>pdfinvoicesAction()</code> in app/code/core/Mage/Adminhtml/controllers/Sales/OrderController.php:</p>
  
  <pre><code>public function pdfRmaAction(){
      $orderIds = $this-&gt;getRequest()-&gt;getPost('order_ids');
      //print_r($orderIds);
      $flag = false;
      if (!empty($orderIds)) {
          foreach ($orderIds as $orderId) {
              $invoices = Mage::getResourceModel('sales/order_invoice_collection')
                  -&gt;setOrderFilter($orderId)
                  -&gt;load();
                  //print get_class($invoices);
                  //print_r($invoices-&gt;getSize());
              if ($invoices-&gt;getSize() &gt; 0) {
                  $flag = true;
                  if (!isset($pdf)){
                      $pdf = Mage::getModel('Nastnet_OrderPrint/order_pdf_order')-&gt;getPdf(array($order));
                  } else {
                      $pages = Mage::getModel('Nastnet_OrderPrint/order_pdf_order')-&gt;getPdf(array($order));
                      $pdf-&gt;pages = array_merge ($pdf-&gt;pages, $pages-&gt;pages);
                  }
              }
          }
          if ($flag) {
              return $this-&gt;_prepareDownloadResponse(
                  'rma'.Mage::getSingleton('core/date')-&gt;date('Y-m-d_H-i-s').'.pdf', $pdf-&gt;render(),
                  'application/pdf'
              );
          } else {
              $this-&gt;_getSession()-&gt;addError($this-&gt;__('There are no printable documents related to selected orders.'));
              $this-&gt;_redirect('*/*/');
          }
      }
      $this-&gt;_redirect('*/*/');
  }
  </code></pre>
  
  <p>This leads to an error in the actual pdf...</p>
  
  <pre><code>Fatal error: Call to a member function getStore() on a non-object in /chroot/home/artizara/dev.artizara.com/html/app/code/local/Nastnet/OrderPrint/Model/Order/Pdf/Order.php on line 60
  </code></pre>
  
  <p>However, if you go into the order and press the <strong>Print RMA</strong> button (instead of trying to Mass Action print it) then it works <strong><em>Just Fine!</em></strong></p>
  
  <p><em>My long winded explanation leads to this:</em> <strong>Why is this error being produced and how can I edit the <code>pdfRmaAction()</code> to work correctly (Mass Action Printing)???</strong></p>