Magento 2 Get All Order Collection with Filters

Method 1:GetAll Order Collection with filters

Directory

VendorName/ModuleName/Block/Orders.php

Content for this file

<?php
namespace VendorName\ModuleName\Block;
class Products extends \Magento\Framework\View\Element\Template
{    
  
     protected $_orderCollectionFactory;

    public function __construct(
        Magento\Framework\App\Action\Context $context,
        \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory 
    ) {
        $this->_orderCollectionFactory = $orderCollectionFactory;
        parent::__construct($context);

    }


   public function getOrderCollection()
   {
       $collection = $this->_orderCollectionFactory->create()
         ->addAttributeToSelect('*')
         ->addFieldToFilter($field, $condition); //Add condition if you wish
     
     return $collection;
     
    }


   public function getOrderCollectionByCustomerId($customerId)
   {
       $collection = $this->_orderCollectionFactory()->create($customerId)
         ->addFieldToSelect('*')
         ->addFieldToFilter('status',
                ['in' => $this->_orderConfig->getVisibleOnFrontStatuses()]
            )
         ->setOrder(
                'created_at',
                'desc'
            );
 
     return $collection;

    }
}

Method 2: Get All Order Collection Filter by Customer

  public function getOrderCollectionByCustomerId($customerId)
   {
       $collection = $this->_orderCollectionFactory()->create($customerId)
         ->addFieldToSelect('*')
         ->addFieldToFilter('status',
                ['in' => $this->_orderConfig->getVisibleOnFrontStatuses()]
            )
         ->setOrder(
                'created_at',
                'desc'
            );
 
     return $collection;

    }

Method 3: Get Order Collection Filter by Date

public function getOrderCollectionByDate($from, $to)
   {
       $collection = $this->_orderCollectionFactory()->create($customerId)
         ->addFieldToSelect('*')
         ->addFieldToFilter('status',
                ['in' => $this->_orderConfig->getVisibleOnFrontStatuses()]
            )
         ->setOrder(
                'created_at',
                'desc'
            );
 
     return $collection;

    }

Method 4: Get Order Collection Filter by Status

public function getOrderCollectionByStatus($statuses = [])
   {
       $collection = $this->_orderCollectionFactory()->create($customerId)
         ->addFieldToSelect('*')
         ->addFieldToFilter('status',
                ['in' => $statuses]
            )
         ;
 
     return $collection;

    }

Method 5: Get Order Collection Filter by Payment Method

public function getOrderCollectionPaymentMethod($paymentMethod)
   {
       $collection = $this->_orderCollectionFactory()->create($customerId)
         ->addFieldToSelect('*')
         ->addFieldToFilter('created_at',
                ['gteq' => $from]
            )
         ->addFieldToFilter('created_at',
                ['lteq' => $to]
            );
 

     /* join with payment table */
    $collection->getSelect()
    ->join(
        ["sop" => "sales_order_payment"],
        'main_table.entity_id = sop.parent_id',
        array('method')
    )
    ->where('sop.method = ?',$paymentMethod); //E.g: ccsave

    $collection->setOrder(
        'created_at',
        'desc'
    );

     return $collection;

    }

 

 

If you are looking for Magento Developer, visit Magento Web Agency.