Magento - Add Custom Mass Action PDF: A Developer’s Guide
As an experienced Magento developer and technical blogger, I’ll provide a comprehensive solution to your question about adding a custom mass action PDF in Magento v1.10.1.0.
The Problem at Hand
You’ve installed the “Admin Order Printing Extension” and customized it to print RMA forms based on invoices for each order. However, you want to extend this functionality to enable mass action printing from the Sales > Orders grid view screen.
Currently, when you choose the “Print RMA” option in the dropdown menu, an error occurs during the actual PDF generation process:
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
However, if you manually go into an individual order and press the “Print RMA” button, it works perfectly fine.
Understanding the Error
The error message suggests that there’s a problem with the store object in your custom PDF model. Specifically, the getStore() method is being called on a non-object, which indicates that the store object hasn’t been properly initialized or retrieved from the session.
To resolve this issue, we need to ensure that the store object is available before trying to access its methods within the PDF generation process in your custom controller and model.
The Solution: Fixing the pdfRmaAction()
In your OrderController.php file, you’ve copied and edited the pdfinvoicesAction() method from the core Sales/OrderController.php. However, there are a few key differences that can cause issues when working with mass action printing.
Here’s an updated version of the pdfRmaAction() method that should resolve the error:
public function pdfRmaAction()
{
$orderIds = $this->getRequest()->getPost('order_ids');
$flag = false;
if (!empty($orderIds)) {
foreach ($orderIds as $orderId) {
$orders[] = Mage::getModel('sales/order')->load((int)$orderId);
}
if (count($orders) > 0) {
$flag = true;
$pdf = Mage::getModel('Nastnet_OrderPrint/order_pdf_order');
foreach ($orders as $order) {
$pages = $pdf->getPdf(array($order));
$pdf->pages = array_merge($pdf->pages, $pages->pages);
}
return $this->_prepareDownloadResponse(
'rma'.Mage::getSingleton('core/date')->date('Y-m-d_H-i-s').'.pdf',
$pdf->render(),
'application/pdf'
);
} else {
$this->_getSession()->addError($this->__('No orders selected.'));
$this->_redirect('*/*/');
}
}
$this->_redirect('*/*/');
}
Here are the key changes I’ve made:
- Load each order using
Mage::getModel('sales/order')->load((int)$orderId)inside a loop. - Check if there are any orders in the array before proceeding with PDF generation.
- Initialize the
$pdfobject outside the foreach loop to avoid re-initialization for each order. - Merge the pages of each order into the existing
$pdf->pagesarray.
By making these adjustments, you ensure that the $order object is properly loaded and available for the PDF model before trying to access its store object in your custom Order.php file.
Conclusion
In this blog post, I’ve provided a thorough solution to your question about adding a custom mass action PDF in Magento. By understanding the error message and making the necessary adjustments to your custom controller and model, you can successfully implement this functionality without any issues.
Remember that working with Magento requires a deep understanding of its core file structure, controllers, models, and block classes. Always be sure to carefully review and test your code before deploying it to a live environment.
If you found this guide helpful, feel free to explore more technical content on Laravel Company. We offer a wide range of tutorials, guides, and best practices for Magento development.
Don’t hesitate to ask if you have any other questions or need further clarification. Happy coding!