Get order data by date in magento2

You can create one html file like "printorder.phtml" on root folder

<!DOCTYPE html>
<html>
  <body style="margin: 0px auto;text-align: center;">
  <h2>Print the Order by Date</h2>
    <form action="orderbydate.php" method="POST">
      Order from:<br>
      <input type="date" name="from" value="">
      <br>
      Last name:<br>
      <input type="date" name="to" value="">
      <br><br>
      <input type="submit" value="Submit">
    </form> 
  </body>
</html>

After that you can just create one php file on root folder "Orderbydate.php"

<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$now = new \DateTime();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
//echo "<pre>"; print_r($orderCollection->getData());
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
 echo '<a href ="printorder.html"> BACK </a> to change the order date';
     $date1 = $_POST['from'];
     $date2 = $_POST['to'];
}else{
 echo '<a href ="printorder.html">Click here </a> to print the order details';die;
}
try

 $time = time();
 //$to = date($date1);
 //$from = date($date2);
 $to = date('Y-m-d H:i:s', strtotime($date1 . '00:00:00') - 19800);
 $from = date('Y-m-d H:i:s', strtotime($date2 . '23:59:59') - 19800);
 $OrderFactory = $objectManager->create('Magento\Sales\Model\ResourceModel\Order\CollectionFactory');
 $orderCollection = $OrderFactory->create()->addFieldToSelect(array('*'));
 $orderCollection->addFieldToFilter('created_at', ['lteq' => $from])->addFieldToFilter('created_at', ['gteq' => $to]);
 ?> 
 <style type="text/css">
  table thead th{
   background: #333333;
   color: #ffffff;   
  }
  table thead th,
  table tbody td{
   font-size: 12px;
  }
 </style>
 <table width="700px" cellpadding="1" cellspacing="0" border="1">
  <thead>
   <tr>
    <th>Order Date</th>
    <th>Order Id</th>
    <th>Order SKUs</th>
    <!-- <th>Customer Name</th> -->
    <th>Payment Method</th>
    <th>Customer Email</th>
    <th>Order Status</th>
    <th>Order Amount</th>
   </tr>
  </thead>
  <tbody>
   <?php
   $order_skus_array = array();  
   foreach ($orderCollection as $order_data) {
    foreach ($order_data->getAllVisibleItems() as $order_items) {
     $order_skus_array[] = $order_items->getSku();
    }
    $payment = $order_data->getPayment();
    $method = $payment->getMethodInstance();
    $methodTitle = $method->getTitle();
    $order_skus = implode(",", $order_skus_array);
   ?>
    <tr>
     <td><?php echo date('Y-m-d H:i:s', strtotime($order_data->getData('created_at')) + 19800); ?></td>
     <td><?php echo $order_data->getData('increment_id'); ?></td>
     <td><?php echo $order_skus; ?></td>
     <td><?php echo $methodTitle; ?></td>
     <td><?php echo $order_data->getData('customer_email'); ?></td>
     <td><?php echo $order_data->getData('status'); ?></td>
     <td><?php echo number_format($order_data->getData('grand_total'), 2, '.', ''); ?></td>
    </tr>
   <?php
   unset($order_skus_array);  
   }
   ?>
  </tbody>
 </table>  
 <?php     

}catch (Exception $e){
 //Mage::log($e->getMessage(),null,'custom_order_export.log');
 print_r($e->getMessage());
}
?>

Comments