Categories: General

How to Export Specific Custom Orders Data Field in CSV from Magento

Having thousands of orders inside your Magento Store makes difficult to look for a specific order data even if you use a backend filter system. And, manually going through all order details is not so easy because there is also so many unnecessary other order related information along with data for which you are actually looking for. Many times it happens that as a store owner, you need very specific data that you have to share with your shipping provider or external services that you are using for your customer. Magento backend doesn’t allow you to export such data from the backend.
At that time getting help from Magento Expert Developer will help you to quickly export such data using some sort of scripts or code. We have come across so many requirements to export some specific order data from Magento. That’s why we are back with another blog series that will help you to quickly export last any number of data with only required fields.
To do the same first we need to create a “orderexport.php” php script using below code.


ini_set('display_errors', 1);
require_once('app/Mage.php');
Mage::app();

try {
    $orders = Mage::getModel('sales/order')->getCollection()
        ->setOrder('created_at', 'desc')
        ->setPageSize(10); //export data from last give orders id  to 10 ex.
    $fp = f0pen(Mage::getBaseDir() . "/orderexport.csv", "w+");
    $csvHeader = array('Order ID', 'Customer Email', 'Customer First Name', 'Customer Last Name'); //CSV Header title
    fputcsv($fp, $csvHeader, ","); //save sequence as give csv header title add more to add both side.
    foreach ($orders as $order) {
        fputcsv($fp, array(
            //save sequence as give csv header title add more to add both side.
            $order->getIncrementId(),
            $order->getCustomerEmail(),
            $order->getCustomerFirstname(),
            $order->getCustomerLirstname()
        ));
    }
    fclose($fp);
    echo 'orderexport.csv Successfully created.';

}catch (\Exception $e){
    echo $e->getMessage();
}
?>

Place this script file in the root server of your Magento and run it by hitting file URL.
I.e. yourstoreurl.com/orderexport.php
That’s it, once a successful script runs it will export order data in CSV file with specified field data. You can find that “orderexport.csv” in the root folder of your hosting. You are free to add or remove order fields that you want to export from your Magento.
Lastly, if you found this blog helpful, don’t forget to share it with your colleagues and Magento Friends and Let us know if you are facing any issue while implementing this code.
Happy Coding!

Click to rate this post!
[Total: 7 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

  • I have copied orderexport.php this file to our root directory, but after hitting not fond page is display.

  • Warning: require_once(app/Mage.php): failed to open stream: No such file or directory in /var/www/html/magento240/orderexport.php on line 3

    Fatal error: require_once(): Failed opening required 'app/Mage.php' (include_path='.:/usr/share/php') in /var/www/html/magento240/orderexport.php on line 3

    I checked in localhost. I got this error. Whether i have to create app/mage.php file?

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

3 hours ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

2 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

2 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

3 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

4 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

6 days ago