0

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.

1

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.

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

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) {
    print_r($order->getData());
}
0

It is working for me.

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

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

    foreach($orderDatamodel as $orderDatamodel1){

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

     }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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