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,[email protected],11800,001,Product Name 1,,020-123-45678 Mr.,John,Doe,[email protected],11800,002,Product Name 2,,020-123-45678 Mr.,John,Doe,[email protected],11800,003,Product Name 3,,020-123-45678 Mr.,Sam,Smith,[email protected],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

1 Answer 1


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

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.

  • ok thank you very much for your help.i will try!
    – strauss
    Nov 21, 2016 at 14:11
  • Do not create a stand alone page, it is a bad practice. Create a module with a route and a controller which return your CSV based on this code. Nov 22, 2016 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.