Magento 2 Orders – Script for Exporting Orders in Magento 2

exportmagento2orders

I'm looking for a script that allows me to retrieve the list of orders for 6 months with the products sold.
The csv file should look like this:

Salutation,First Name,Last name,Email,Transaction Id,Product Id,Product name,Transaction Timestamp,Telephone

Mr.,John,Doe,JohnDoe@example.com,11800,001,Product Name 1,,020-123-45678
Mr.,John,Doe,JohnDoe@example.com,11800,002,Product Name 2,,020-123-45678
Mr.,John,Doe,JohnDoe@example.com,11800,003,Product Name 3,,020-123-45678
Mr.,Sam,Smith,SSmith@example.com,11801,001,Product Name 1,,
,Jane,,Jane1985@example,11802,001,Product Name 1,009287654321
,Jane,,Jane1985@example,11802,002,Product Name 2,009287654321.

Being a beginner on magento2, I do not know how to develop the script myself.
I hope you can help me

Thank you very much

Best Answer

You can create a stand alone page on root of the magento folder and proceed in this manner :

<?php 
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';


$orderData = getOrderCollection();
echo '<pre>';print_r($orderData);die;       
function getOrderCollection()
    {
        $bootstrap = Bootstrap::create(BP, $_SERVER);
        $objectManager = $bootstrap->getObjectManager();
        $order = $objectManager->create('Magento\Sales\Model\Order')->getCollection();
        //echo '<pre>';print_r($order->getData());die;      
        foreach($order as $_order)
        {
            $orderData[$_order->getId()]['customer_firstname'] = $_order->getData('customer_firstname');        
            $orderData[$_order->getId()]['customer_lastname'] = $_order->getData('customer_lastname');
            $orderData[$_order->getId()]['customer_email'] = $_order->getData('customer_email');
            $orderItems = $_order->getAllItems();
            foreach($orderItems as $item)
            {
                //echo '<pre>';print_r($item->getData());die;                       
                $orderData[$_order->getId()][$item->getId()]['item_name'] = $item->getName();
            }

        }


        return $orderData;

    }

You can filter the order collection as you want and also get data as per your requirements.

Related Topic