Magento 2 – How to Get Order Data

magento2magento2.2orderssales-order

How get all orders data in custom page in magento2

$orderDatamodel = $objectManager->get('Magento\Sales\Model\Order')->getCollection();
 foreach($orderDatamodel as $orderDatamodel1){
 print_r($orderDatamodel1->getData());

 }

Nothing displaying. Please help me.

Best Answer

I was facing same issue but when I added

ini_set('display_errors', 1);

in top of the page, I got below error.

Fatal error:  Allowed memory size of 2621440000 bytes exhausted (tried to allocate 12288 bytes)  

You can try below code

$orders = $obj->get('Magento\Sales\Model\Order')->getCollection()->setPageSize(10) // only get 10 products 
    ->setCurPage(1);

echo "<pre>";
foreach ($orders as $order) {
    print_r($order->getData());
}
echo "</pre>";

This is same method you are using to print data with filter and it is working fine.

There are 2 solutions, you can increase memory limit or select fields that you want in your collection.

In addition, use the $order->debug() method instead of $order->getData(). This will prevent memory leaks.

Related Topic