How get all orders data in custom page in magento2

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


Nothing displaying. Please help me.

3 Answers 3


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 

echo "<pre>";
foreach ($orders as $order) {
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.

  • 1
    In addition, use the $order->debug() method instead of $order->getData(). This will prevent memory leaks. Feb 27, 2018 at 14:00
  • Thanks simonthesorcerer, i have add your comment in my answer Feb 28, 2018 at 6:20

It is working for me.

$appState = $objectManager->get('\Magento\Framework\App\State');

$orderDatamodel = $objectManager->get('\Magento\Sales\Model\Order')->getCollection();

    foreach($orderDatamodel as $orderDatamodel1){

       echo '<pre>'; print_r($orderDatamodel1->getData());


Use Magento\Sales\Model\ResourceModel\Order\Collection instead of Magento\Sales\Model\Order and remember to set the area code:

$appState = $objectManager->get("Magento\Framework\App\State")->setAreaCode("frontend");
$orders   = $objectManager->create("Magento\Sales\Model\ResourceModel\Order\Collection");

foreach ($orders as $order) {

Your Answer

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

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