在业务管理过程中,Magento 2商店可能需要过滤所有订单集合以进行进一步分析。在今天的文章中,我想向您展示如何通过按客户、日期、状态和付款方式进行筛选来获取所有订单集合

如何获取所有订单集合

  • 方法1:通过过滤器获取所有订单集合
  • 方法2:按客户ID获取所有订单集合
  • 方法3:按日期获取订单集合
  • 方法4:按状态获取订单集合
  • 方法5:按付款方式获取订单集合

方法一:通过过滤器获取所有订单集合

要获取带有过滤器的所有订单集合,请转到以下路径Example/HelloWorld/Block/Orders.php并创建一个Orders.php文件。

<?php
namespace Example\HelloWorld\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;

    }
}

方法2:按客户获取所有订单集合过滤器

在前端开发 Magento 2 扩展(例如“我的帐户”)时,您可能需要在后端的网格中显示现有的客户订单。以下是我们如何按客户获取所有订单过滤器:

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

    }

方法3:按日期获取订单集合过滤器

除了按客户获取订单收集过滤器之外,您还可以按日期获取订单收集过滤器。我们是这样做的:

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

    }

方法4:按状态获取订单集合过滤器

获取订单收集过滤器的第四种方法是按状态。

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

    }

方法5:按付款方式获取订单收集过滤器

除了上述解决方案可以帮助您获得订单收集过滤器之外,还有另一种方法。以下指南将向您展示如何按付款方式获取订单收集过滤器。

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;

    }