Customizing Magento Invoice Excel Export
Exporting invoices from your Magento store is a common requirement for accounting purposes or data analysis. However, the standard export format may not always meet your specific needs. If you find that you need additional columns in your invoice exports, this guide will walk you through the process of customizing the output.
Understanding the Export Process
Before we dive into the code, let’s first understand where the exported files are generated and how Magento handles this process. By default, when you initiate an export from the Admin panel (Sales > Invoices > Export), Magento creates a CSV file named invoices.csv in the following directory:
/var/export/invoice/
The CSV format is chosen because it’s compatible with most spreadsheet programs like Microsoft Excel or OpenOffice Calc. However, if you prefer XML as your export format, you can change this by modifying the InvoicesController.php file located at app/code/core/Mage/Adminhtml/controllers/Sales/OrderController.php.
Customizing the Invoice Export Columns
To add new columns to your invoice exports, you’ll need to modify the way Magento generates the CSV content. This involves updating the exportAction() method in the InvoicesController.php file.
The relevant part of the code is located within a loop that iterates through each invoice:
$csv->addColumn('invoice_id', array(
'label' => Mage::helper('sales')->__('Invoice #'),
'index' => 'entity_id',
));
// Add your custom columns here
$csv->addRow(array(
'invoice_id' => $row->getId(),
));
To include a new column, follow these steps:
-
Determine the attribute or field you want to export. In this case, let’s assume we want to include the invoice’s total tax amount.
-
Add a new array to the
$csv->addColumn()method with the appropriate parameters:'label': A human-readable name for the column.'index': The attribute code for the field you want to export.
Here’s how you can add a new column for the tax amount:
$csv->addColumn('tax_amount', array(
'label' => Mage::helper('sales')->__('Tax Amount'),
'index' => 'tax_amount',
));
-
Make sure your custom columns are added before the
$csv->addRow()statement. -
Save your changes and clear the Magento cache to ensure the modifications take effect.
Verifying Your Changes
After you’ve made these adjustments, initiate an invoice export from the Admin panel once again. The new CSV file should now contain the additional column(s) you specified.
For a more advanced approach, consider creating a custom module that extends the Mage_Adminhtml module and provides your own export functionality. This allows for greater flexibility and keeps your core files untouched. You can learn more about developing custom Magento modules on our Magento Development Guide.
Conclusion
Customizing invoice exports in Magento requires a good understanding of the export process and some PHP coding knowledge. By following this guide, you should now be able to add or modify columns in your exported invoices with ease.
Remember that modifying core files can potentially cause issues if not done carefully. Always backup your code before making changes, and consider using a module for more complex customizations.
Happy exporting!