Including Product SKUs in Magento Orders CSV
Exporting orders from your Magento store is a common task, but by default, the generated CSV file does not include the SKU of the products within each order. In this post, we’ll guide you through the process of modifying the export to include product SKUs.
Understanding the Default Export Process
Magento uses a built-in module called Mage_Adminhtml for exporting orders. The default configuration only includes basic information such as order ID, status, and customer details. Product-specific data like SKU is not included by default.
Modifying the Orders Grid to Include SKU
To include product SKUs in your export, you’ll need to modify the orders_grid_collection collection in app/code/core/Mage/Adminhtml/Model/Resource/Reports/Order/Collection.php.
- Open your Magento installation and navigate to
app/code/core/Mage/Adminhtml/Model/Resource/Reports/Order/Collection.php. - Locate the
_afterLoadmethod. - Add the following code inside this method:
$collection->getSelect()->joinLeft(
array('order_items' => $collection->getTable('sales/order_item')),
'main_table.entity_id = order_items.order_id',
array('sku' => 'order_items.sku')
);
This line of code joins the sales/order_item table to the main collection and adds a new column called sku from the joined table.
Modifying the Orders Export CSV Format
After adding the SKU field to your orders grid, you need to modify the CSV export format to include this data. You’ll do this in the Mage_Adminhtml_Model_Sales_Order_Csv_Export model.
- Open
app/code/core/Mage/Adminhtml/Model/Sales/Order/Csv/Export.php. - In the
getHeader()method, add the following line:
$this->_columns['sku'] = array(
'label' => Mage::helper('sales')->__('SKU'),
'index' => 'sku',
);
This adds a new column to your CSV header with the label “SKU” and maps it to the sku field in the collection.
Exporting Your Orders
With these modifications, you can now export your orders including product SKUs. To do this:
- Log into your Magento admin panel.
- Navigate to System > Reports > Orders.
- Click on the “Export” button at the top right of the page.
- Choose your desired export format (CSV).
- Click “OK” to start the download.
The exported CSV file will now include a new column labeled “SKU”, which contains the SKUs of all products associated with each order.
Conclusion
Including product SKUs in your Magento orders export provides valuable data for inventory management and analysis. By following this guide, you’ve successfully modified your store’s core functionality to meet your specific needs.
Remember, always backup your files before making changes to your codebase. If you’re unsure about any part of the process or need further assistance, don’t hesitate to reach out to our team at Laravel Company.
Happy exporting!