We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

    I need to get order collection between specific date range passing start date and end date in Magento 2. For that I used filter created_at field using addAttributeToFilter().

    First create Block file By default created_at field in sales_order table represent the time of order creation:

    <?php
    namespace VenderModulenameBlock;
    class OrderRange extends MagentoFrameworkViewElementTemplate
    {
     public function __construct(
     MagentoFrameworkViewElementTemplateContext $context,
     MagentoSalesModelResourceModelOrderCollectionFactory $orderCollectionFactory,
     array $data = []
     ) {
     $this->orderCollectionFactory = $orderCollectionFactory;
     parent::__construct($context, $data);
     }
    
     /* Here Create Function For Order collection between start and end date */
     public function getOrderCollectionByDateRange(){
     $startDate = date("Y-m-d h:i:s",strtotime('0000-00-00')); // start date
     $endDate = date("Y-m-d h:i:s", strtotime('0000-00-00')); // end date
    
     $orders = $this->orderCollectionFactory->create()
     ->addAttributeToFilter('created_at', array('from'=>$startDate, 'to'=>$endDate));
     return $orders;
     }
    ?>

    After do this You need to call this function in any custom template file or any exsting template,

    if($orders->getTotalCount() > 0) { 
     foreach($orders as $_order) {
     echo "<pre>";print_r($_order); echo "</pre>";
     }
    }

    Now you can get Order collection by date range.

    Fill the below form if you need any Magento relate help/advise/consulting.

    With Only Agency that provides a 24/7 emergency support.

      Get a Free Quote