Magento Tutorials

How to Export Orders to CSV in Magento 2

This guide is gonna walk you through how to export orders in Magento into CSV file, starting from the easiest way by using Magento backend, to a more advanced way of using script. Either way, you will end up with a CSV file containing all the data from your Orders grid.

Export orders from Magento backend

In your Magento backend, go to Sales > Orders.

Sales orders

In the Orders panel that follows, you should have the following columns by default:

  • ID
  • Purchase Point
  • Purchase Date
  • Bill-to Name
  • Ship-to Name
  • Grand Total (Base)
  • Grand Total (Purchased)
  • Status
  • Action
  • Allocated sources
  • Braintree Transaction Source

Here you can Export your Orders data by clicking on the Export drop down and tick CSV

magento 2 orders export

Click Export and the generated CSV will contain the data in this Default view

Export orders programmatically with custom data

Alternatively, you can also programmatically add new columns in your Orders grid and run Observer to export Orders CSV.

Create new column in vendor/magento/module-sales/view/adminhtml/ui_component/sales_order_grid.xml

<column name="custom_product_name" class="Magento\Sales\Ui\Component\Listing\Column\Price">
        	<argument name="data" xsi:type="array">
            	<item name="config" xsi:type="array">
                	<item name="filter" xsi:type="string">textRange</item>
                	<item name="visible" xsi:type="boolean">false</item>
                	<item name="label" xsi:type="string" translate="true">Custom Product Name</item>
            	</item>
        	</argument>
</column>

Make changes to the sales_order_grid table by running the following upgrade schema:

ALTER TABLE `sales_order_grid` ADD `custom_product_name` VARCHAR( 255 ) NOT NULL ;

Run Observer when an order is submitted successfully

For example, in events.xml:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">

    <event name="checkout_submit_all_after">
        <observer name="yourcompany_yourmodule_checkout_submit_all_after" instance="YourCompany\YourModule\Observer\ProcessOrder" />
    </event>

</config>

And in the observer:

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $order = $observer->getOrder();
    $quote = $observer->getQuote();

    // Do whatever you want here

    return $this;
}

Click Export.

Export orders using an extension 

As the default orders export option in Magento is still pretty limited, the better way to do this would be to use an extension to have the job done for you. There are various order export extensions available on the marketplace (both free & paid) for you to choose from.

Hope this helped!


See also:
How to Export Products to CSV in Magento 2
How to Refund an Order in Magento 1 & 2

Is it helpful?

A knowledge craver who always strive to be wiser everyday.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments