Adding Order Print Feature to Magento Admin
As an experienced developer, I frequently receive requests to enhance Magento’s admin functionality. One common requirement is adding a print order feature directly from the sales > orders section. In this tutorial, we’ll guide you through the process of implementing this feature step-by-step.
Understanding the Default Functionality
By default, Magento provides limited printing options for orders in the admin panel. You can only manually print the invoice or shipment using the ‘Print’ button available on the specific order view page. However, our goal is to create a dedicated “Order Print” button that appears directly from the sales > orders grid.
Step 1: Create the Print Order Controller
The first step involves creating a new controller that will handle the print action for the selected orders. In your Magento installation’s root directory, navigate to the app/code folder and create a new module if you haven’t already:
<your_namespace>/OrderPrint/etc
<your_namespace>/OrderPrint/Controller/Adminhtml/Order
# app/code/<your_namespace>/OrderPrint/Controller/Adminhtml/Order/Print.php
The contents of the Print.php file should look like this:
namespace <YourNamespace>\OrderPrint\Controller\Adminhtml\Order;
use Magento\Framework\App\ActionInterface;
use Magento\Backend\App\Action;
use Magento\Framework\View\Result\PageFactory;
use Magento\Sales\Model\Order\Pdf\Items;
use Magento\Sales\Model\Order\Pdf\Invoice;
class Print extends Action implements ActionInterface
{
protected $_resultPageFactory = false;
public function __construct(Action\Context $context, PageFactory $resultPageFactory)
{
parent::__construct($context);
$this->_resultPageFactory = $resultPageFactory;
}
public function execute()
{
$orderId = (int)$this->getRequest()->getParam('order_id');
$order = $this->_objectManager->create('Magento\Sales\Model\Order')->load($orderId);
if ($order) {
$pdf = new Invoice();
$items = new Items($order);
$pdf->setItems($items);
// Additional settings for the PDF
$pdf->setPdfTitle(str_replace(['[store]', '[date]'], date('Y-m-d H:i:s'), 'Sales Order'));
$pdf->render();
return $this->_fileFactory->create(
'invoice.pdf',
$pdf->getFile(),
DirectoryList::PDF,
null
);
}
throw new \Magento\Framework\Exception\LocalizedException(__('We can\'t find an order with the specified ID.'));
}
}
Step 2: Create the Print Order Button
To add a “Print Order” button to the sales > orders grid, we need to modify the sales_order_grid.xml layout file:
# app/code/Magento/Sales/view/adminhtml/layout/sales_order_grid.xml
Add the following code inside the <reference name="content"> block:
<block type="core/text" name="print.order.button">
<action method="setText"><text><![CDATA[Print Order]]></text></action>
<action method="setClass"><class>action-print</class></action>
<action method="setOnClickJs"><script>window.open('${base_url}admin/<your_namespace>/orderprint/order/print/order_id/$_item.order_id', '_blank')</script></action>
</block>
Step 3: Configure the Button in the Grid
Finally, we need to configure the new button for the sales > orders grid:
# app/code/Magento/Sales/etc/adminhtml/menu.xml
Add the following code inside the <menuItems> section:
<item name="print_order" type="action">
<url path="" module="<your_namespace>/orderprint" controller="Order" action="print"/>
</item>
Step 4: Clear Magento Cache
After completing these steps, don’t forget to clear your Magento cache for the changes to take effect.
Conclusion
By following this comprehensive guide, you should now have successfully added a “Print Order” button to the sales > orders grid in your Magento admin panel. This feature allows administrators to quickly generate and print order documents directly from the grid view without navigating to individual order pages.
Remember, always test changes thoroughly before deploying them to a live environment. If you require assistance with more complex or customized solutions, feel free to reach out to the development team at Laravel Company.
Happy coding! Let me know if there’s anything else I can help with.